~/ ~/documents ~/pictures ~/harmful.txt github

Impure Flake Inputs

Software builds with impurities produce non-deterministic and inconsistent outputs, making it impossible to guarantee reproducibility. Impure inputs should be avoided wherever possible.

Impure inputs can be introduced into flakes by using the --override-input flag to override a flake input with an impure source. This allows for the injection of non-deterministic state into evaluation:

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  inputs.IMPURE.url = "file+file:///dev/null";
  inputs.IMPURE.flake = false;

  outputs = { self, nixpkgs, IMPURE }: {
    packages.impure = nixpkgs.legacyPackages.x86_64-linux.stdenvNoCC.mkDerivation {
      name = "impure";
      dontUnpack = true;
      buildPhase = ''
        echo ${builtins.getEnv "IMPURE"} > $out
      '';
    };
  };
}

When evaluating the impure package, pass the --override-input flag to inject an impure value for the IMPURE input of the flake and the --no-update-lock-file flag to prevent modification of the flake.lock file:

nix build --no-update-lock-file --override-input IMPURE file+file://<(printf %s "foo") && cat result # outputs "foo"