Flakes: How to build/run packages/apps other than the defaults?

I originally asked this in a GitHub issue for nmattia/naersk and it was suggested that I ask it here instead. I followed these instructions on using naersk with flakes.

nix build and nix run work fine since defaultPackage = packages.my-project; and defaultApp = apps.my-project; are set.

However, nix build my-project and nix run my-project (and the .#my-project variants) result in:

error: --- Error ------------------------------------------------------------------------------------------------------------------------------------- nix
cannot find flake 'flake:my-project' in the flake registries

I’m using a Cargo workspace and trying to build packages/apps that aren’t the defaults.

Here are the docs:

nix run some-flake#app-name
nix build some-flake#package-name

Few examples:
nix run github:gytis-ivaskevicius/nixfiles#g-neovim
nix run /etc/nixos#g-neovim
cd /etc/nixos && nix run .#g-neovim

Thanks, but I’ve read those and I was seeing this error despite that which is why I opened the GitHub issue.

To rephrase, my issue appears to be with how to target a package/app in a flake.nix like the one I linked to above.

For example, this gives me the error I shared:

  1. $ cd <directory containing flake.nix with my-project under packages and apps>
  2. $ nix run .#my-project

I really can’t imagine how does one get such an error with hashtag syntax…

One of the issues I can think of is # not being escaped (in shell scripts # is a comment)
try these:

cd /etc/nixos && nix run .\#g-neovim
cd /etc/nixos && nix run '.#g-neovim'

And are you sure that thats the error you are getting?

1 Like

I can only think about a single circumstance when that error might happen when using .#anything.

You use flake:my-project as an input in your flake, be it as an URL like inputs.foo.url = "flake:my-project"; (or without flake:) or as an implicit input in the argset of the outputs function.

Can you therefore please share all the entries for inputs and the argument set of the outputs function?

Alternatively please share again the exact command you use as well as the exact error message. Please do not edit or obfuscate either.

1 Like

Escaping the pound sign using either method makes things work as expected. Thanks @gytis-ivaskevicius!

FWIW I’m using zsh on macOS.

1 Like