Skip to content

Quickly create NixOS snapshots on Hetzner Cloud (with Packer)

Notifications You must be signed in to change notification settings

kyswtn/create-hcloud-nixos-snapshot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quickly create a NixOS snapshot on Hetzner Cloud using Packer.

create-hcloud-nixos-snapshot \
  --location hel1 \
  --server-type cax11 \
  --ssh-key "$SSH_KEY"

#  --host-name nixos
#  --save-config-to $PWD
#  --help

If the script succeeds, it'll save generated hardware-configuration.nix file in current working directory or directory configured via --save-config-to. Once created, the snapshot can be referenced by tools such as Terraform during provisioning pipeline.

The script is exported at packages.${system}.default and can be installed via Nix flakes. For example,

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    chns = {
      url = "github:kyswtn/create-hcloud-nixos-snapshot";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = { self, nixpkgs, chns, ... }:
    let
      system = "aarch64-darwin";
      nixpkgs = nixpkgs.legacyPackages.${system};
      create-hcloud-nixos-snapshot = chns.packages.${system}.default;
    in
    {
      devShells.${system}.default = pkgs.mkShellNoCC {
        packages = [
          create-hcloud-nixos-snapshot
        ];
      };
    };
}

Generated snapshot is intentionally as minimal as possible, and only installs SSH with Public Key Authentication and Tailscale. Once a machine has been provisioned based off the snapshot, use nixos-rebuild with --target-host to rebuild with desired new configuration.

This script installs NixOS exactly how the official documentation guides you to. If nixos-anywhere or nixos-infect is too overwhelming, this is for you. Until Hetzner officially provides NixOS standard images, this script will be handy. Especially for those who don't want to maintain extra configurations.

About

Quickly create NixOS snapshots on Hetzner Cloud (with Packer)

Resources

Stars

Watchers

Forks