From a5724314d05bb917e1b6fef1472ceac80b1b2f52 Mon Sep 17 00:00:00 2001 From: Adrian Gierakowski Date: Mon, 2 Feb 2026 18:17:16 +0100 Subject: [PATCH 1/2] Allow consuming flakes with impure derivations - If any package or check in the consumed flake has `__impure = true`, propagate this attribute to the `devour-output.json` derivation using `lib.overrideDerivation`. - Add a test case with an impure flake to verify attribute propagation and successful building. - Enable required experimental Nix features in CI (`impure-derivations`, `ca-derivations`). --- .github/workflows/ci.yaml | 3 +++ flake.nix | 32 ++++++++++++++++++++++++++++---- test/impure_flake/flake.nix | 18 ++++++++++++++++++ test/test.sh | 5 ++++- test/test_impure.sh | 20 ++++++++++++++++++++ 5 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 test/impure_flake/flake.nix create mode 100755 test/test_impure.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bb968c0..771b292 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,5 +12,8 @@ jobs: - uses: actions/checkout@v3 - name: Install Nix uses: DeterminateSystems/nix-installer-action@main + with: + extra-nix-config: | + experimental-features = nix-command flakes impure-derivations ca-derivations - name: Test run: ./test/test.sh diff --git a/flake.nix b/flake.nix index 3d4720b..94d88a8 100644 --- a/flake.nix +++ b/flake.nix @@ -57,7 +57,7 @@ perSystemPaths ++ flakePaths; # Build result organized by system - result = builtins.listToAttrs (map (sys: + buildResultForSystem = sys: let allPaths = collectPathsForSystem sys; uniquePaths = lib.unique allPaths; @@ -67,11 +67,35 @@ in if name == null then acc else acc // { "${name}" = path; } ) { } outPaths; in - { name = sys; value = { inherit outPaths byName; }; } - ) build-systems); + { inherit allPaths outPaths byName; }; + + resultsBySystem = map (sys: { name = sys; value = buildResultForSystem sys; }) build-systems; + + result = builtins.listToAttrs (map (item: + { + name = item.name; + value = { inherit (item.value) outPaths byName; }; + } + ) resultsBySystem); + + hasImpure = drv: + if lib.isDerivation drv + then drv.__impure or false + else false; + + isImpure = lib.any (item: lib.any hasImpure item.value.allPaths) resultsBySystem; in { - default = pkgs.writeText "devour-output.json" (builtins.toJSON result); + default = + let + drv = pkgs.writeText "devour-output.json" (builtins.toJSON result); + in + if isImpure + then + lib.overrideDerivation drv (old: { + __impure = true; + }) + else drv; } ); }; diff --git a/test/impure_flake/flake.nix b/test/impure_flake/flake.nix new file mode 100644 index 0000000..91c8af4 --- /dev/null +++ b/test/impure_flake/flake.nix @@ -0,0 +1,18 @@ +{ + outputs = { self }: + let + systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ]; + forAllSystems = f: builtins.listToAttrs (map (name: { inherit name; value = f name; }) systems); + in + { + packages = forAllSystems (system: { + impure = derivation { + name = "impure"; + inherit system; + builder = "/bin/sh"; + args = ["-c" "echo impure > $out"]; + __impure = true; + }; + }); + }; +} \ No newline at end of file diff --git a/test/test.sh b/test/test.sh index 194c004..c0280a9 100755 --- a/test/test.sh +++ b/test/test.sh @@ -7,4 +7,7 @@ nix build .. --override-input flake github:srid/haskell-multi-nix/c85563721c3886 diff expected result -rm -f result \ No newline at end of file +rm -f result + +# Run impure flake tests +./test_impure.sh \ No newline at end of file diff --git a/test/test_impure.sh b/test/test_impure.sh new file mode 100755 index 0000000..3ccde64 --- /dev/null +++ b/test/test_impure.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -euxo pipefail + +# Navigate to test directory +cd "$(dirname "$0")" + +echo "Running impure flake test..." + +nix build .. --override-input flake path:./impure_flake + +impure=$(nix eval ..#default.__impure --override-input flake path:./impure_flake) + +if [[ $impure == true ]]; then + echo "PASS: Derivation has __impure set to true." +else + echo "FAIL: Derivation does NOT have __impure set to true." + exit 1 +fi + +echo "Impure flake test passed!" From 0a784aed449a99d102ca4f117f9c20db7573f684 Mon Sep 17 00:00:00 2001 From: Adrian Gierakowski Date: Mon, 2 Feb 2026 18:21:16 +0100 Subject: [PATCH 2/2] Ignore Nix build result symlinks --- .github/workflows/ci.yaml | 2 +- .gitignore | 2 ++ test/impure_flake/flake.nix | 2 +- test/test.sh | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 771b292..11d0e31 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,7 +13,7 @@ jobs: - name: Install Nix uses: DeterminateSystems/nix-installer-action@main with: - extra-nix-config: | + extra-conf: | experimental-features = nix-command flakes impure-derivations ca-derivations - name: Test run: ./test/test.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..750baeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +result +result-* diff --git a/test/impure_flake/flake.nix b/test/impure_flake/flake.nix index 91c8af4..78bb35a 100644 --- a/test/impure_flake/flake.nix +++ b/test/impure_flake/flake.nix @@ -15,4 +15,4 @@ }; }); }; -} \ No newline at end of file +} diff --git a/test/test.sh b/test/test.sh index c0280a9..6acb73f 100755 --- a/test/test.sh +++ b/test/test.sh @@ -10,4 +10,4 @@ diff expected result rm -f result # Run impure flake tests -./test_impure.sh \ No newline at end of file +./test_impure.sh