Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impurity: Hash of relative paths depends on parent directory name #166

Closed
rickynils opened this issue Sep 4, 2013 · 3 comments
Closed

Comments

@rickynils
Copy link
Member

Consider the following Nix expression, stored in default.nix:

with (import <nixpkgs> {});

writeText "out" ''
  ${./.}
''

Then look at the following console session:

/tmp/nixtest $ nix-build --no-out-link
these derivations will be built:
  /nix/store/wvarg2d7f0dhfgz0wvwhm8ibzkb1pfrc-out.drv
building path(s) `/nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out'
�building /nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out
�/nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out
/tmp/nixtest $ cd ..
/tmp $ mv nixtest/ nixtest2
/tmp $ cd nixtest2/
/tmp/nixtest2 $ nix-build --no-out-link
these derivations will be built:
  /nix/store/piwxsvbn6myy4xajazg0pwlh7k46sfzq-out.drv
building path(s) `/nix/store/4z5vzja4gk0mifrn4dxd2d17fwfsy86w-out'
�building /nix/store/4z5vzja4gk0mifrn4dxd2d17fwfsy86w-out
�/nix/store/4z5vzja4gk0mifrn4dxd2d17fwfsy86w-out
/tmp/nixtest2 $ cd ..
/tmp $ mv nixtest2/ nixtest
/tmp $ cd nixtest/
/tmp/nixtest $ nix-build --no-out-link
/nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out

As you can see, the hash of the build depends on the name of directory that contains default.nix. I had expected that only the files and directories contained inside this directory would matter. This is an impurity, I would say, since you can't recreate a build if you don't know the name of the directory that the build was originally performed in.

@edolstra
Copy link
Member

edolstra commented Sep 4, 2013

That's not really an impurity. As long as the name and contents of the file are the same, you should get the same hash. That the name might change is simply a reason not to use ./. in this case.

@rickynils
Copy link
Member Author

But the parent directory is not really part of the content, I think. If I clone Nix sources from github, should it really impact my build what I named the target directory?

My example may seem contrived, but I discovered this when building NixOS. I keep different NixOS configurations in the following directory layout:

/.../machine1/default.nix
/.../machine1/configuration.nix
/.../machine1/nixos
/.../machine1/nixpkgs
/.../machine2/default.nix
/.../machine2/configuration.nix
/.../machine2/nixos
/.../machine2/nixpkgs

My default.nix files contains the following:

let
  nixos = import ./nixos {
    nixpkgs = ./nixpkgs;
    configuration = ./configuration.nix;
  };
in {
  inherit (nixos) system;
}

Then I can build a system in the following way:

cd machine1
nix-build -A system

But if I change the names of the machine1 or machine2 directories, I get a rebuild even though nothing that should have an impact on the build has changed.

@edolstra
Copy link
Member

Closing since this is intended (if not always desirable) behaviour.

zolodev pushed a commit to zolodev/nix that referenced this issue Jan 1, 2024
rpi: suggest updating firmware in case image doesn't boot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants