NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.47k stars 1.5k forks source link

Cannot implicitly copy oddly-named files to nix store #6515

Open tejing1 opened 2 years ago

tejing1 commented 2 years ago

Describe the bug

Attempting to implicitly copy files to the nix store fails when the file's basename does not conform the the sanity requirements imposed on nix store paths.

Steps To Reproduce

$ touch test\ file
$ nix eval --impure --expr '"${./. + "/test file"}"'
error: store path 'rj4w2hxhanlcasa863zqln3yv98v6v61-test file' contains illegal character ' '

Expected behavior

Nix should sanitize the basename before constructing the store path, such that this operation always succeeds, just with a sanitized store path.

This shouldn't cause a reproducibility problem if implemented, since the behavior only differs in situations where there would have been a fatal error before. It's still fail-or-succeed-correctly.

nix-env --version output

Tried with 2.8.0, still a problem.

Additional context

I found this problem while trying to track down why a friend couldn't place his network manager connection files in /etc using the environment.etc options in configuration.nix. It was because the files were named after the SSIDs, and the SSIDs have unfortunate characters in their names. I was able to work around it with builtins.toFile + builtins.readFile + lib.strings.sanitizeDerivationName, but this really should "just work".

matthewbauer commented 2 years ago

You can use builtins.path to specify a custom name to use. I think that's more efficient than builtins.toFile + builtins.readFile.

tejing1 commented 1 year ago

Still a problem.

arichtman commented 1 year ago

I think I'm having the same issue but it's fetchUrl that's not sanitizing the file name

error: store path 'g179hjgfa2g6fji4s3gw1aq7hvxzkq1k-hugo,rust,linux,macos,direnv,python,windows,terraform,terragrunt,rust-analyzer,dotnetcore' contains illegal character ','

image

URL-encoding it still threw an error, just over the % character

tejing1 commented 1 year ago

Just add a name field to the fetchurl args attrset. Should solve it. But yes, fetchurl should really sanitize the name as well.

arichtman commented 1 year ago

Thanks! I was able to get it working. I can see the argument for not wanting to apply a lossy sanitizing algorithm by default. Have a great day!