From db74dfd85d65a76bfb4b0ffb816f2a16999ad6a3 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 26 Oct 2024 10:10:17 -0400 Subject: [PATCH 1/3] nix: Restrict OMNIX_SOURCE further to avoid rebuilds Also, re-use it in rust-project.src so we can test it as part of normal Nix build. --- crates/omnix-cli/crate.nix | 11 +------ nix/modules/flake-parts/om.nix | 4 --- nix/modules/flake-parts/rust.nix | 55 ++++++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/crates/omnix-cli/crate.nix b/crates/omnix-cli/crate.nix index 66dad486..aeda6b8e 100644 --- a/crates/omnix-cli/crate.nix +++ b/crates/omnix-cli/crate.nix @@ -50,16 +50,7 @@ in CACHIX_BIN ; - # To avoid unnecessary rebuilds, start from cleaned source, and then add the Nix files necessary to `nix run` it. Finally, add any files required by the Rust build. - OMNIX_SOURCE = lib.cleanSourceWith { - src = inputs.self; - filter = path: type: - rust-project.crane-lib.filterCargoSources path type - || lib.hasSuffix ".nix" path - || lib.hasSuffix "flake.lock" path - || lib.hasSuffix "registry.json" path - ; - }; + OMNIX_SOURCE = rust-project.src; # Disable tests due to sandboxing issues; we run them on CI # instead. diff --git a/nix/modules/flake-parts/om.nix b/nix/modules/flake-parts/om.nix index 79ae9ac9..050d58fe 100644 --- a/nix/modules/flake-parts/om.nix +++ b/nix/modules/flake-parts/om.nix @@ -14,10 +14,6 @@ # name = "default"; args = [ "show" "." ]; }; - build-omnix-source = { - type = "app"; - name = "build-omnix-source"; - }; binary-size-is-small = { type = "app"; name = "check-closure-size"; diff --git a/nix/modules/flake-parts/rust.nix b/nix/modules/flake-parts/rust.nix index feea7e4e..c19b3d3f 100644 --- a/nix/modules/flake-parts/rust.nix +++ b/nix/modules/flake-parts/rust.nix @@ -8,12 +8,41 @@ inputs.process-compose-flake.flakeModule inputs.cargo-doc-live.flakeModule ]; - perSystem = { config, self', pkgs, ... }: { + perSystem = { config, self', pkgs, lib, ... }: { cargo-doc-live.crateName = "omnix-gui"; rust-project = { # See /crates/*/crate.nix for the crate-specific Nix configuration crateNixFile = "crate.nix"; + + # To avoid unnecessary rebuilds, start from cleaned source, and then add the Nix files necessary to `nix run` it. Finally, add any files required by the Rust build. + src = + let + # Like crane's filterCargoSources, but doesn't blindly include all TOML files! + filterCargoSources = path: type: + config.rust-project.crane-lib.filterCargoSources path type + && !(lib.hasSuffix ".toml" path && !lib.hasSuffix "Cargo.toml" path); + in + lib.cleanSourceWith { + src = inputs.self; + filter = path: type: + filterCargoSources path type + || lib.hasSuffix "registry.json" path + || lib.hasSuffix "crate.nix" path + || "${inputs.self}/flake.nix" == path + || "${inputs.self}/flake.lock" == path + # Select *only* the non-Rust files necessary to build omnix package. + || lib.hasSuffix "flake-parts/nixpkgs.nix" path + || lib.hasSuffix "flake-parts/rust.nix" path + || lib.hasSuffix "tests/flake.nix" path + || lib.hasSuffix "tests/flake.lock" path + || lib.hasSuffix "failing/flake.nix" path + || lib.hasSuffix "registry/flake.nix" path + || lib.hasSuffix "registry/flake.lock" path + || lib.hasSuffix "flake-schemas/flake.nix" path + || lib.hasSuffix "flake-schemas/flake.lock" path + ; + }; }; packages = @@ -33,18 +62,16 @@ }); }; - # Make sure that `OMNIX_SOURCE` is buildable. - # We must check this in CI, because it is built only during runtime otherwise (as part of `om ci run --on`). - apps.build-omnix-source = { - meta.description = "Build OMNIX_SOURCE"; - program = pkgs.writeShellApplication { - name = "build-omnix-source"; - runtimeInputs = [ pkgs.nix ]; - text = '' - set -x - nix build -L --no-link --print-out-paths ${self'.packages.omnix-cli.OMNIX_SOURCE.outPath} - ''; - }; - }; + checks.omnix-source-has-flake = pkgs.runCommand + "omnix-source-has-flake" + { buildInputs = [ pkgs.lsd ]; } '' + set -e + cd ${self'.packages.omnix-cli.OMNIX_SOURCE.outPath} + pwd + lsd --tree + test -f flake.nix + test -f flake.lock + touch $out + ''; }; } From 7bbc8784c2ca0cc4754107a3ff9ab32fefe27520 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 26 Oct 2024 10:25:37 -0400 Subject: [PATCH 2/3] ci(nix): Check that $OMNIX_SOURCE is buildable Do it without building it; just evaluating the drv is enough. --- nix/modules/flake-parts/om.nix | 4 ++++ nix/modules/flake-parts/rust.nix | 24 +++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/nix/modules/flake-parts/om.nix b/nix/modules/flake-parts/om.nix index 050d58fe..d4a4995a 100644 --- a/nix/modules/flake-parts/om.nix +++ b/nix/modules/flake-parts/om.nix @@ -19,6 +19,10 @@ name = "check-closure-size"; systems = [ "x86_64-linux" ]; # We have static binary for Linux only. }; + omnix-source-is-buildable = { + type = "app"; + name = "omnix-source-is-buildable"; + }; cargo-tests = { type = "devshell"; command = [ "just" "cargo-test" ]; diff --git a/nix/modules/flake-parts/rust.nix b/nix/modules/flake-parts/rust.nix index c19b3d3f..d35ac69a 100644 --- a/nix/modules/flake-parts/rust.nix +++ b/nix/modules/flake-parts/rust.nix @@ -62,16 +62,18 @@ }); }; - checks.omnix-source-has-flake = pkgs.runCommand - "omnix-source-has-flake" - { buildInputs = [ pkgs.lsd ]; } '' - set -e - cd ${self'.packages.omnix-cli.OMNIX_SOURCE.outPath} - pwd - lsd --tree - test -f flake.nix - test -f flake.lock - touch $out - ''; + apps.omnix-source-is-buildable.program = pkgs.writeShellApplication { + name = "omnix-source-is-buildable"; + runtimeInputs = [ + pkgs.jq + ]; + text = '' + set -e + cd ${self'.packages.omnix-cli.OMNIX_SOURCE.outPath} + # Make sure the drv evaluates (to test that no files are accidentally excluded) + nix --accept-flake-config --extra-experimental-features "flakes nix-command" \ + derivation show "." | jq -r '.[].outputs.out.path' + ''; + }; }; } From e7dc2aaa1e14c752b21bf3b2d223c6bf2a22b2a2 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 26 Oct 2024 10:28:34 -0400 Subject: [PATCH 3/3] nix: Add toolchain so $OMNIX_SOURCE builds --- nix/modules/flake-parts/rust.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nix/modules/flake-parts/rust.nix b/nix/modules/flake-parts/rust.nix index d35ac69a..adeab4e7 100644 --- a/nix/modules/flake-parts/rust.nix +++ b/nix/modules/flake-parts/rust.nix @@ -31,6 +31,7 @@ || lib.hasSuffix "crate.nix" path || "${inputs.self}/flake.nix" == path || "${inputs.self}/flake.lock" == path + || "${inputs.self}/rust-toolchain.toml" == path # Select *only* the non-Rust files necessary to build omnix package. || lib.hasSuffix "flake-parts/nixpkgs.nix" path || lib.hasSuffix "flake-parts/rust.nix" path