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

Add a derefPath function that copies a path to the store #37730

Closed
wants to merge 1 commit into from

Conversation

TomSmeets
Copy link
Contributor

This function will copy a path to the nix store. The difference with
using src = ./. is how symbolic links are handled. Nix normally just copies those links as-is into the store.
This function can be used to dereference all symbolic links.

Example

{ stdenv, derefPath }:
stdenv.mkDerivation rec {
  name = "hello";

  src = derefPath {
    name = "${name}-src";
    src = ./.;
  };
}

This is a quick and dirty fix for the following issues in NixOS/nix:

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option build-use-sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

This function will copy a path to the nix store. The difference with
using `src = ./.` is how symbolic links are handled. Nix normally just copies those links as-is into the store.
This function can be used to dereference all symbolic links.

```
{ stdenv, derefPath }:
stdenv.mkDerivation rec {
  name = "hello";

  src = derefPath {
    name = "${name}-src";
    src = ./.;
  };
}
```

This is a quick and dirty fix for the following issues in `NixOS/nix`:
* NixOS/nix#1232
* NixOS/nix#897
@langston-barrett
Copy link
Contributor

langston-barrett commented May 10, 2018

I'm having trouble composing this with sourceFilesBySuffix:

if lib.inNixShell                                                                
    then null                                                                        
    else lib.flip lib.sourceFilesBySuffices tex_exts (derefPath {                    
      name = "${name}-src";                                                          
      src  =  ./.;                                                                   
    });

yields

string '/nix/store/2vlz4rdfjmd0amjvic8g1msdsyyqw128-thesis-src' cannot refer to other paths, at /nix/store/xcxw5jil05wn652fp2c2g7na1zcbjkpd-source/lib/sources.nix:45:17

Any tips? Am I doing this wrong, or is there a way to improve the compositionality of this function?

Also, some documentation might be good!

EDIT: I can confirm that this does work well for its intended purpose. Thank you for sharing!

EDIT 2: There's actually one other small issue: when the content backing the symlink is updated, Nix doesn't notice and rebuild src.

@djahandarie
Copy link
Contributor

@peti Does this seem like a decent first step to dealing with NixOS/nix#897 and NixOS/nix#1232 to you?

@djahandarie
Copy link
Contributor

Though, I think this might not work correctly with sandboxing, because it creates a derivation which the sandboxed builder runs and tries to cp from the src path, which may not be in the nix store.

@Mic92 Mic92 requested a review from edolstra December 12, 2018 17:04
@Mic92
Copy link
Member

Mic92 commented Dec 12, 2018

Disadvantage of course is that it has two copy stuff twice.

@edolstra
Copy link
Member

I don't understand the name. "Dereferencing a path" means something like readFile to me.

Also this would be better to handle in Nix (maybe as a flag to builtins.path?).

@Mic92
Copy link
Member

Mic92 commented Dec 12, 2018

Yeah. Something like builtins.path { ...; resolveSymlinks = true; }.

@Mic92
Copy link
Member

Mic92 commented Dec 12, 2018

I don't think this should be part of nixpkgs. This of course nobody stops it from copying the function into their own projects.

@Mic92 Mic92 closed this Dec 12, 2018
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-stop-nix-from-unpacking-a-directory/6866/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants