···2525 "first machine in the data center" problem: in bare-metal setups with
2626 persistent PXE servers, you would otherwise need to automate the installation
2727 of the PXE server itself.
2828-[^2]: Using the provided examples with a warm cache. Factors that affect the
2929- speed include whether the configuration has been cached in the Nix store,
3030- the size of the NixOS configuration, the network speed, etc.
2828+[^2]: Using the provided examples with a warm cache on a 1 Gbps network.
2929+ Factors that affect the speed include whether the configuration has been
3030+ cached in the Nix store, the size of the NixOS configuration, the network
3131+ speed, etc.
31323233## Usage
3334
+5
cmd/nixie/main.go
···7878 }()
7979 log.Info("API server started", "address", address)
80808181+ // TODO IMPORTANT obviously, should be API call
8282+ // TODO IMPORTANT support SSH key
8383+ log.Info("installing NixOS", "host", "192.168.1.8", "flake", "./examples#machine1")
8484+ nixos.Install(ctx, "./examples#machine1", "root", "192.168.1.8", "nixos-installer", flags.Debug)
8585+8186 sigCh := make(chan os.Signal, 1)
8287 signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
8388
···11+package nixos
22+33+import (
44+ "context"
55+ "fmt"
66+ "os"
77+ "os/exec"
88+)
99+1010+func Install(ctx context.Context, flakeRef, user, host, password string, debug bool) error {
1111+ cmd := exec.CommandContext(
1212+ ctx,
1313+ "nixos-anywhere",
1414+ "--flake", flakeRef,
1515+ "--target-host", fmt.Sprintf("%s@%s", user, host),
1616+ "--env-password",
1717+ // In the case of PXE boot, where target machines are usually on the same LAN as the one running Nixie,
1818+ // pushing from the Nix store where Nixie is running is usually faster than pulling from a remote cache over the internet.
1919+ // Additionally, it's air-gapped.
2020+ "--no-substitute-on-destination",
2121+ )
2222+2323+ cmd.Env = append(os.Environ(), fmt.Sprintf("SSHPASS=%s", password))
2424+2525+ if debug {
2626+ cmd.Stdout = os.Stdout
2727+ cmd.Stderr = os.Stderr
2828+ }
2929+3030+ return cmd.Run()
3131+}