Trying to create a self-contained nixos-anywhere install

I have my (private) GitHub repo with a flake, configurations, and everything. I’m able to (mostly) zero-touch deploy this configuration to any VM (local VMs using tart, VMs on my network, remove VMs in the cloud), and it’s awesome!

The issue that I have is for after the install. My goal (and maybe it’s the wrong goal) is to be able to install to a VM, and then be able to utilize nixos-rebuild on the VM to apply any configuration changes. nixos-anywhere doesn’t include the configuration repository, so now I’m stuck going down the path of how to get my repository onto the VM.

I’m not using nixos-rebuild from my local machine because it’s a Mac, and just has Nix. I’m not seeing where I’m able to “build” a self-contained ISO because of being on an ARM Mac with an intel Linux target.

From the best that I can tell (and mind you, I’m very new to Nix and NixOS), I can create a package that uses fetchFromGitHub to stick my configuration repository in the Nix store.

I guess my questions are

  1. Is this the best way of doing this?
  2. Am I going about this (my goal) entirely the wrong way?
  3. What is everyone else doing for this?

Thanks!

1 Like

That’s one way, but it’s a somewhat weird way of doing it, since now you cannot change your configuration and only statically rebuild what you already have deployed.

It’d make more sense to write a systemd unit to download it (with WantedBy=first-boot.target or such, or even on a timer to keep it updated).

Personally I think I’d just use nixos-anywhere’s file upload support

Finally, you can just run nixos-rebuild --flake github:your/repo and change the VM config that way. I think you can even set the autoUpgrade timers to do that, then you’d effectively have a gitops setup, just need an access key or SSH key with read permissions on the VM (which can be installed with nixos-anywhere, and/or sops-nix/agenix).


All that said cross compilation is very much possible. I’ve never tried how much effort it’d be to do this, but you should be able to use nixos-rebuild 's --remote-target, or something like deploy-rs.

And finally, I’m pretty sure making self-contained ISOs should be pretty simple too, this repo has all the builders for various image formats: GitHub - nix-community/nixos-generators: Collection of image builders [maintainer=@Lassulus]

Now that you put it out there, I think I really have 2 separate issues that I’m trying to solve for. A production deployment, where I have my configuation.nix that I’m happy with, I really like the nixos-rebuild --flake github:your/repo solution of just pointing the configuration directly at GitHub.

The other issue (and I think closer to my original struggles) was how to do “rapid development” and iterate quickly on my configuration.nix. To that regard, I actually just found rsync to be a super simplistic solution to getting everything on my testing VM.

In case any others stumble across this in the future, what I’m running is

rsync --delete --mkpath --compress --recursive . root@$(tart ip nixos):/nix/persist/config && \
  ssh root@$(tart ip nixos) "nixos-rebuild switch --flake /nix/persist/config"

If you have ssh access to the root user, why not just use nixos-rebuild switch --target-host and maybe --build-host if you really wanted the remote to build as well?

Only because I’m not on a NixOS host, so I don’t have access to nixos-rebuild

nixos-rebuild is just a script, you can run it from anywhere that you can use nix.

1 Like

Oh? :thinking: Apologies for my naivete, but where can I get the script? I have Nix installed on my macbook, but nixos-rebuild isn’t in my path.

Like any other software[1], nix-shell -p nixos-rebuild.


  1. Assuming, like nixos-rebuild, that it’s a self-contained binary that doesn’t require system configuration or graphics drivers. ↩︎

2 Likes

Well damn, I should’ve known it was that easy…I love Nix! :tada:

1 Like

For anyone else on mac, this is my one-liner

nix-shell -p nixos-rebuild --run "nixos-rebuild switch --flake . --fast --build-host root@$(tart ip nixos) --target-host root@$(tart ip nixos)"
1 Like