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

There exists a [mutitude](https://github.com/NixOS/nixops)
[of](https://github.com/DBCDK/morph) [tooling](https://github.com/krebs/krops)
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](https://nixos.org/manual/nixos/stable/#sec-installation), and copy over
the generated configuration to `config/<hostname>/configuration.nix`.

On the root directory create `flake.nix`:

```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](https://nixos-and-flakes.thiscute.world/), but
haven't read it

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