Skip to content

Instantly share code, notes, and snippets.

@thufschmitt
Created May 15, 2023 08:13
Show Gist options
  • Save thufschmitt/b8936f2184c68f4b7c3ea0fe294b64e2 to your computer and use it in GitHub Desktop.
Save thufschmitt/b8936f2184c68f4b7c3ea0fe294b64e2 to your computer and use it in GitHub Desktop.
Discard references in Nix
let p = import <nixpkgs> {}; in
let
discardReferences = path:
let
# We want to use a different nixpkgs here to make sure that the `noref`
# version won't accidentally share some build-time references with the
# original one
p2 = (builtins.getFlake "github:nixos/nixpkgs/nixos-unstable").legacyPackages.${builtins.currentSystem};
mangledPath = p2.runCommandNoCC "${path.name}.tar.gz" {} ''
cd ${path}
tar -cz . > $out
'';
mangledPathNoRef = builtins.seq
(builtins.pathExists mangledPath)
(/${builtins.unsafeDiscardStringContext mangledPath});
pathNoRef = p2.runCommandNoCC "${path.name}-noref" { passthru.original = path; } ''
mkdir -p $out
cd $out
tar -xzf ${mangledPathNoRef}
'';
in pathNoRef;
in discardReferences p.hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment