Skip to content

Instantly share code, notes, and snippets.

@ruuda
Last active September 29, 2023 07:03
Show Gist options
  • Save ruuda/c17509f6684e6e01bc9017a39d86addf to your computer and use it in GitHub Desktop.
Save ruuda/c17509f6684e6e01bc9017a39d86addf to your computer and use it in GitHub Desktop.
Monadic Nix Example
{ pkgs ?
import (fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/13b2903169f18ac98ed737effb36dabc48051978.tar.gz";
sha256 = "1ql9ychp7w7ciqyb7am22b9hrlsz2sppgjflgr0ffw92gvwi3m64";
}) {}
}:
with pkgs;
let
outer = stdenv.mkDerivation {
name = "outer";
phases = ["buildPhase"];
buildPhase = ''
mkdir -p $out
cat << EOF > $out/default.nix
with (import (fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/13b2903169f18ac98ed737effb36dabc48051978.tar.gz";
sha256 = "1ql9ychp7w7ciqyb7am22b9hrlsz2sppgjflgr0ffw92gvwi3m64";
}) {}
);
stdenv.mkDerivation {
name = "inner";
phases = ["buildPhase"];
# Is this bad? Now the store path depends on when you first ran nix-build!
# Note that this is not a non-reproducible build of "inner", it is an issue
# with "outer".
buildPhase = "mkdir -p \$out; echo 'hi from inner at $(date)' > \$out/hi.txt";
}
EOF
'';
};
in
import outer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment