blogng/blog/2023-11-14-easy-nixos-remote-deployments.markdown
2024-01-11 13:27:05 +01:00

2.1 KiB

layout title date comments tags
post Easy NixOS remote deployments 2023-11-14 true nixos, nix, flakes

There exists a mutitude of tooling to remotely manage nixos machines, each with its own features and compromises.

In my experience, for a simple deployment of few hosts nixos-rebuild --target-host is pretty powerful.

This is the workflow I've been using to manage my personal systems:

I bootstrap the machines manually by following the nixos install guide, and copy over the generated configuration to config/<hostname>/configuration.nix.

On the root directory create flake.nix:

{
  description = "systems needed";
  inputs = {
     # extra inputs go here
  };

  outputs = { self, nixpkgs }@attrs: {
    # this is where we add new machines
    nixosConfigurations = {
     # host fancyHostname
      fancyHostname = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        specialArgs = attrs;
        modules = [
          # This points to the actual machine configuration
          (import ./config/nixmachine/configuration.nix)
        ];
      };
    };
  };
}

From there, hosts can deployed over SSH!

nixos-rebuild switch  --target-host root@fancyHostname --flake '.#fancyHostname'

More hosts can be added by adding them to the nixosConfigurations attribute set.

nixosConfigurations = {
    fancySecondHost = { ... }
    ...
}

Should I do this?

I think this is an easier workflow for me to manage and debug, However a lot of things in this space has non-linear learning curves and has sparse documentation at best.

So maybe a tool with better documentation might work well for you?

This also relies heavily on flakes, which is "experimental". In my experience, ground reality is everyone uses flakes, but good documentation is hard to come by

I've herd good things about Nix Flakes Book, but haven't read it

This was first published at https://pencil.lalalala.in/dbalan/easy-nixos-remote-deployments