diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bb968c0..11d0e31 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-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/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..78bb35a --- /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; + }; + }); + }; +} diff --git a/test/test.sh b/test/test.sh index 194c004..6acb73f 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 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!"