Skip to content

Instantly share code, notes, and snippets.

@YuMingLiao
Created August 26, 2024 01:24
Show Gist options
  • Select an option

  • Save YuMingLiao/18b09ed4acde010d7704deb1b9d354be to your computer and use it in GitHub Desktop.

Select an option

Save YuMingLiao/18b09ed4acde010d7704deb1b9d354be to your computer and use it in GitHub Desktop.
with builtins;
with (import <nixpkgs> { });
with (import <nixpkgs> { }).lib.attrsets;
with (import <nixpkgs> { }).stdenv;
with rec {
deepReadDir = path:
let
attrset = readDir path;
go = name: value:
if value == "directory" then
deepReadDir (path + "/${name}")
else
path + "/${name}";
in mapAttrs go attrset;
freezeFiles = path: freezeFileSet (deepReadDir path);
freezeFileSet = attrset:
let
go = _: filepath:
builtins.path {
path = filepath;
sha256 = narHash filepath;
};
in mapAttrsRecursive go attrset;
narHash = p:
builtins.convertHash {
hash =
#hashFile "sha256" p; # it is flat while nix-freeze-files uses recursive.
readFile (hashPath p);
toHashFormat = "nix32";
hashAlgo = "sha256";
};
hashPath = p:
mkDerivation {
__contentAddressed = true;
name = baseNameOf p + "-hashPath";
src = dirOf p;
buildInputs = [ nix ];
buildPhase = ''
touch $out
nix-hash --type sha256 --base32 ${baseNameOf p} | tr -d "\n" > $out
'';
};
}; {
inherit freezeFiles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment