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.