Getting the flake "xxxx-source" path

I’m trying to the path to the source of the flake (e.g. /nix/store/xxxxx-source).
Currently I’m using nix flake metadata | grep "Path" | awk '{print $2}'.
Is there a canonical way to do this?

Context:
I’m writing a small deployment script, which copies the flake and point nixos-rebuild --flake [path] to it:

source=`nix flake metadata | grep "Path" | awk '{print $2}'`
nix copy --to ssh-ng://user@server $source
ssh -t user@server nixos-rebuild switch --flake $source

Would nixos-rebuild with the --target-host do what you’re looking for with less trouble?

I’m hoping to run the builds on the target machine. I’m primarily on macOS so can’t compile for x86-64_linux.

There’s nixos-rebuild --build-host and most other deployment tools are able to do this too.

I use colmena for other deployments, but want to try deploying without tools, seeing how easy it is with passing --flake to nixos-rebuild

nixos-rebuild isn’t available on mac running Nix. I think I need to push the flake to the remote server, and then trigger rebuild there via SSH, unless there’s some another command which can trigger a rebuild from a non NixOS machine.

I could just use scp and upload it to some random location… but using nix copy / nix-copy-closure is neat since it’d be removed when I run nix-collect-garbage periodically.

Note that nixos-rebuild itself is also a deployment tool. It’s a bash script, so you can see what commands it runs quite easily. It’s not actually very complicated at its core.

It sure is: nix-shell -p nixos-rebuild. I used this to deploy my Linux server from macOS for a long time until I switched to colmena.

There is no need to do such a thing. Either nix-instantiate locally, copy the resulting .drv closure to the builder and then build the copied .drv on the target or use nix-build's --store/NIX_REMOTE.

Always use Nix commands to handle Nix store paths. nix-copy-closure/nix copy can copy any Nix store path to any other Nix store.

2 Likes

It sure is: nix-shell -p nixos-rebuild.

Ahh that’s neat. Didn’t expect that it’s a bash script under the hood.

Thanks, I’ll give it another try with the new knowledge.

Some links in case anyone is looking to do the same:

Please note that on macOS you also need to provide the [--fast flag]