NixOps

From NixOS Wiki
Revision as of 20:05, 11 November 2024 by Sshine (talk | contribs) (Alter formulation in warning slightly)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

NixOps is a tool for deploying NixOS systems in a reproducible and declarative manner. It allows users to manage and deploy entire NixOS-based infrastructures, whether to cloud platforms, virtual machines, or physical hardware.

Warning: at this time NixOps is not actively recommended for new projects or users. The tool is undergoing a significant transition between major versions, which has led to some instability and increased complexity in getting it up and running. As of August 2024, it requires extra effort to set up and maintain, and it might not offer a smooth experience. See nixops #1574 for updates and details or check out the nixops4 project.

NixOps allows users to declaratively specify the desired configuration of their systems and then automatically performs all necessary actions to realize that configuration. This includes tasks such as instantiating cloud machines, managing dependencies, and provisioning resources. NixOps is meant to be fully automated and to create reproducible deployments that leverage the Nix package manager’s purely functional model, creating consistency in the configuration and providing reliability across various environments.

For further details, please refer to the NixOps manual, which provides an overview of its functionality and features, as well as an up-to-date installation guide.

Example Configuration

This example demonstrates a basic NixOps configuration that sets up a staging environment with two machines: a reverse proxy and an application server running a git server (Forgejo). This example assumes that both machines already exist, that SSH in the operator's machine is well configured to reach them, and that both machines are running NixOS. The nix language allows referencing the configuration of other machines using the nodes argument, making it easy to link services across the network.

# network-staging.nix file
let
  proxyHostname = "proxy.example.com";
  gitHostname = "10.0.0.2";
in {
  network.description = "Staging environment for our git setup";
  defaults.imports = [ ./common.nix ];
  
  reverse-proxy = { nodes, ... }: {
    deployment.targetHost = proxyHostname;
    services.nginx = {
      enable = true;
      virtualHosts."example.com".locations."/" = {
        proxyPass = "http://${gitHostname}:${nodes.gitServer.config.services.forgejo.port}";
      };
    };
    # the rest of reverse-proxy's configuration can be added here
  };
  
  gitServer = _: {
    deployment.targetHost = gitHostname;
    services.forgejo.enable = true;
    # additional git server configuration can be added here
  };
}

Invocation

To apply this configuration on both nodes, one must first create a deployment with the nixops create command, and then apply the new configuration with nixops deploy.

nixops create network-staging.nix -d staging
> created deployment ‘32b06868-d27c-11e2-a055-81d7beb7925e’

nixops deploy -d staging

External links

See also