9

I'd like to add functionality relating to the awscli command completion.

In my particular case I need to source the script from /nix/store/hvx7xqvjz7r08nsb9kssh1d9s302v3sp-awscli-1.16.106/share/zsh/site-functions/aws_zsh_completer.sh.

How can I get the store path (/nix/store/hvx7xqvjz7r08nsb9kssh1d9s302v3sp-awscli-1.16.106) for a package (awscli)? As I don't think hardcoding this reference is ideal.

5 Answers5

6

You can use configuration.nix to create a symlink in /etc that points to the file you want to source. Then you simply source said symlink. For example, something like this...

configuration.nix

environment.etc."zsh/zshrc".source = "${pkgs.awscli}/share/zsh/site-functions/aws_zsh_completer.sh";

...would create the symbolic link /etc/zsh/zshrc which would point to /nix/store/hvx7xqvjz7r08nsb9kssh1d9s302v3sp-awscli-1.16.106/share/zsh/site-functions/aws_zsh_completer.sh

Since this is done through configuration.nix, the symbolic link will get updated to the correct path each time you build the system.

Then, you can source /etc/zsh/zshrc in your ~.zshrc.

4

Through nix-locate , install it then update the database with nix-index:

nix-locate "PATTERN"
GAD3R
  • 66,769
2

You could use nix eval --raw to print the store path of a package. For your example:

ls $(nix eval --raw nixpkgs.awscli)/share/zsh/site-functions/aws_zsh_completer.sh

This file will only exist if the package is already in your local nix store.

⚠️ Update in June 2022 with nix 2.8.0

This command no longer works as-is. I tried to updated it and came up with this:

ls $(nix --extra-experimental-features nix-command --extra-experimental-features flakes eval -f '<nixpkgs>' --raw 'awscli')/share/zsh/site-functions/aws_zsh_completer.sh

For what it's worth, if you're trying to set up your shell initialization to reference nix packages, the workaround I'm using at the moment is to do this:

export NIX_PROFILE_ROOT=${NIX_PROFILES[(w)2]}
maybesource () {
    [[ -r $1 ]] && . $1
}

And then reference packages via the nix profile:

maybesource $NIX_PROFILE_ROOT/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
jbyler
  • 121
  • 1
    I'm not sure if this has changed since this answer was posted, but for me I need to use eg nix eval --raw nixpkgs#awscli - note the hash instead of a period. – Chris J Harris Mar 17 '22 at 00:41
1

For anyone else looking to do this in Bash with newer versions of Nix, use nix eval -f '<nixpkgs>' --raw PACKAGE.

For example, nix eval -f '<nixpkgs>' --raw firefox or nix eval -f '<nixpkgs>' --raw xorg.libX11.

l0b0
  • 51,350
0

I had a different problem, but this is in the top DDG results for nix store reference so I'll leave this here. My problem wasn't so much that I needed to reference a specific file from another package in my package, I just needed my package to have a rundep (buildInputs) on particular store paths so I could install it to my profile and they wouldn't be GC'd. (I was sick of constantly re-downloading paths when using nix-shell, and while nix-env -iA nixpkgs.stdenv worked, nix-env -iA nixpkgs.bash is apparently a different copy.)

Just putting the store paths into a text file in $out doesn't work, they need to be passed through builtins.storePath first:

writeTextDir "share/nix-shell-nogc" (builtins.concatStringsSep "\n" (builtins.map builtins.storePath [
    "/nix/store/2r4gr92p49kpx48s0hxxxv04cqbx4d6c-bash-interactive-5.1-p16-dev"
    "/nix/store/76yin93c7c0jv29kq2hr39d5rzd0v6dg-bash-interactive-5.1-p16-doc"
    "/nix/store/d2m4s6bm6gxpwd22pv20cbhl3ikbhhpb-bash-interactive-5.1-p16-man"
    "/nix/store/p5fqinp9r82fr92rncwxmhd8nsk3igjy-bash-interactive-5.1-p16-info"
]))

Or alternatively, use path literals (doesn't build unless the paths exist, while the above method downloads them during build if necessary):

writeTextDir "share/nix-shell-nogc" ''
    ${/nix/store/2r4gr92p49kpx48s0hxxxv04cqbx4d6c-bash-interactive-5.1-p16-dev}
    ${/nix/store/76yin93c7c0jv29kq2hr39d5rzd0v6dg-bash-interactive-5.1-p16-doc}
    ${/nix/store/d2m4s6bm6gxpwd22pv20cbhl3ikbhhpb-bash-interactive-5.1-p16-man}
    ${/nix/store/p5fqinp9r82fr92rncwxmhd8nsk3igjy-bash-interactive-5.1-p16-info}
'';

I don't like that I had to hardcode the paths, but it gets the job done.