Nix configuration for NixOS and home-manager
  • Nix 77%
  • Shell 18.2%
  • Lua 2.5%
  • Just 1.1%
  • FLUX 0.6%
  • Other 0.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Simon Gate b736957dc9
Some checks failed
nix-ci / pr-diff-fennel (push) Has been skipped
nix-ci / pr-diff-fly (push) Has been skipped
nix-ci / pr-diff-leek (push) Has been skipped
nix-ci / pr-diff-paprika (push) Has been skipped
nix-ci / pr-diff-smgt-dev (push) Has been skipped
nix-ci / pr-diff-termite (push) Has been skipped
nix-ci / pr-diff-yam (push) Has been skipped
nix-ci / build-fennel (push) Successful in 12s
nix-ci / build-fly (push) Successful in 29s
nix-ci / build-leek (push) Successful in 14s
nix-ci / build-paprika (push) Successful in 13s
nix-ci / build-smgt-dev (push) Successful in 13s
nix-ci / build-termite (push) Failing after 3s
nix-ci / build-yam (push) Failing after 5s
inputs: update smgtvim
2026-11-06 09:15:36 +01:00
.forgejo/workflows ci: remove moth 2026-09-14 08:07:41 +02:00
bin remove tmx errors 2026-06-02 21:00:04 +02:00
config/terraform Initial import 2025-09-26 10:56:15 +02:00
docs fennel: setup atproto pds (#64) 2026-09-30 21:34:16 +02:00
home hm: split vcs settings into general and dev 2026-11-06 09:15:01 +01:00
lib yam: on-demand agent microVM for yam 2026-11-06 09:15:01 +01:00
machines termite: enable codesearch, terraform, vikunja mcp 2026-11-06 09:15:01 +01:00
microvms agents: add .agents dir temporarily mount in and tests things 2026-11-06 09:15:01 +01:00
modules nix: configure substituters priority 2026-09-30 21:19:57 +02:00
pkgs pkgs: use external monstar flake 2026-09-24 09:48:19 +02:00
scripts ci: allow 201 response 2026-08-01 00:07:33 +02:00
tests yam: start the agents microVM automatically 2026-11-06 09:15:01 +01:00
.envrc Initial import 2025-09-26 10:56:15 +02:00
.gitignore fennel: setup atproto pds (#64) 2026-09-30 21:34:16 +02:00
AGENTS.md project: update AGENTS.md 2026-11-06 09:15:01 +01:00
flake.lock inputs: update smgtvim 2026-11-06 09:15:36 +01:00
flake.nix yam: on-demand agent microVM for yam 2026-11-06 09:15:01 +01:00
iso.nix general cleanup 2026-02-18 15:41:52 +01:00
justfile dev-vps (#35) 2026-07-14 22:27:54 +02:00
proxmox.md Initial import 2025-09-26 10:56:15 +02:00
README.md yam: start the agents microVM automatically 2026-11-06 09:15:01 +01:00

Nix

Home-manager standalone

home-manager build --flake .#[system name]
home-manager switch --flake .#[system name]

NixOS

Installation on hardware

Build ISO

# Inside the nix/ directory
export NIX_PATH=nixos-config=$PWD/iso.nix:nixpkgs=channel:nixos-25.11
nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage

This will result in a ISO with settings from the iso.nix file. After the build is done you can write the ISO image to a USB stick or similar. The build command will output the Nix store path and it will also be available in ./result/iso/.

Boot and provision

Boot the ISO on the new system, find the host name and provision the device using nixos-anywhere.

First create a configuration for the system in machines/[name]/default.nix. Then add the system to flake.nix. After that is done you can provision the device. We also generate the hardware configuration for the system.

# Example with disk encryption and sops
# --disk-encryption-keys [target host key location] [current host key location]
nix run github:nix-community/nixos-anywhere -- \
  --flake .#[system name]\
  --disk-encryption-keys /tmp/secret.key <(cat /secret/password) \
  --generate-hardware-config nixos-generate-config machines/[system name]/hardware-configuration.nix \
  --copy-host-keys \
  --target-host simon@[ip address]

# Example without disk encryption
nix run github:nix-community/nixos-anywhere -- \
  --flake .#[system name]\
  --generate-hardware-config nixos-generate-config machines/[system name]/hardware-configuration.nix \
  --target-host simon@[ip address]

Reboot the system.

Making changes to a system

Update a remote system.

nix run nixpkgs#nixos-rebuild -- \
  --target-host simon@[hostname] \
  --sudo \
  switch \
  --flake .#[system name]

Update a local system

sudo nixos-rebuild switch --flake .#[systemname]

Ephemeral development VPS

The dev-vps profile is a generic NixOS configuration for short-lived VPSes created by another tool and converted from Ubuntu with nixos-anywhere. It uses an unencrypted full-disk disko layout on /dev/nvme0n1 and enables Tailscale without joining the tailnet automatically.

Provision an AWS VPS with EC2 boot defaults:

nix run github:nix-community/nixos-anywhere -- \
  --flake .#dev-vps-aws \
  --target-host root@[ip address]

For non-AWS providers that also expose the root disk as /dev/nvme0n1, use .#dev-vps. The configured root partition uses 100% of the remaining disk after the EFI partition.

After the install, join Tailscale manually with a one-off, tagged, non-ephemeral auth key. Replace the hostname with a useful per-instance name if you want it to show up that way in Tailscale.

ssh simon@[ip address]
sudo tailscale up \
  --auth-key="$TS_AUTHKEY" \
  --advertise-tags=tag:dev-vps \
  --hostname=smgt-1-2-3-4

Configure Tailscale ACLs so trusted devices can reach tag:dev-vps on TCP port 22, without granting tag:dev-vps outbound access to other tailnet services.

Agent microVM on yam

yam includes a QEMU/KVM guest named agents (4 vCPUs, 8 GiB RAM) that starts automatically at boot. OpenCode2 is installed/configured on yam using smgtllm and installed in the guest through Home Manager. The guest runs the server; the host runs the client and editor. Only the two yam user keys from lib/yam-ssh-keys.nix can log in.

Before deploying, check id -u simon on yam: this configuration pins simon's UID to 1000. If the existing UID differs, adjust it in machines/yam/agents.nix before switching; do not change an existing account's UID without migrating files. Also check ip route for conflicts with the dedicated 10.99.0.0/30 subnet.

Build/evaluate without starting the guest:

nix develop -c nix eval --impure --expr 'import ./tests/agents.nix (builtins.getFlake (toString ./.))'
nix develop -c nix build --no-link .#nixosConfigurations.yam.config.microvm.vms.agents.config.config.system.build.toplevel
nix develop -c nix eval --raw .#nixosConfigurations.yam.config.system.build.toplevel.drvPath

Deploy yam through the normal NixOS rebuild workflow. Then, on yam:

sudo systemctl start microvm@agents
ssh agents

The dedicated TAP link uses 10.99.0.1 on yam and 10.99.0.2 in the guest. ssh agents is an alias for ssh simon@10.99.0.2, without SSH-agent forwarding. Outbound traffic is NATed through yam's current uplink; no SSH or API port forwarding is needed. The guest firewall accepts SSH/API traffic only from 10.99.0.1, and the API additionally requires the generated password.

From a project directory on yam:

cd ~/code/my-project
OPENCODE_PASSWORD="$(cat ~/microvm/agents/secrets/password)" \
  opencode2 --server http://10.99.0.2:43827

Use the remote client to sign in to model providers. Authentication and sessions are written by the guest server to host-backed storage; recreating the guest does not require signing in again. Existing host-server sessions/authentication are not automatically imported. Do not point another server at this database.

Storage and isolation

Host path Guest use
~/code Same path, read/write; includes untracked files and project secrets
~/.config/opencode Same path, read-only; managed by smgtllm on yam
/nix/store Read-only lower layer of the guest store
~/microvm/agents/opencode ~/.local/share/opencode: sessions, provider auth, logs
~/microvm/agents/ssh Persistent SSH host keys, root-owned
~/microvm/agents/secrets Generated server password, mounted read-only
~/microvm/agents/nix Read-only host store validity snapshot, refreshed at VM start
~/microvm/agents/backups Not mounted in the guest

The guest has its own Nix daemon and database, not the host daemon socket. Its 32 GiB sparse root disk at /var/lib/microvms/agents/root.img preserves the guest home, cache, Nix database and writable store overlay across starts. No disk is automatically reset. OpenCode data and SSH host keys remain in the separate host-backed directories above, so they also survive replacing the root disk. An older ephemeral-store.img, if present, is left untouched; it is not a root disk and must not be renamed to root.img.

The guest can read host store contents but cannot write them. Automatic host GC is skipped while this VM runs; do not run manual host GC while it is running. Paths added to the host store after the validity snapshot may require restarting the VM for Nix to recognize them. New dependencies can be downloaded/built inside the guest instead. At boot the guest verifies store registrations and repairs dependencies removed by host GC while it was stopped. Repair may require Internet access, take time, or block Nix/OpenCode startup if a dependency cannot be recovered or the disk is full. Config/skills referencing other host paths need explicit shares; private flake inputs will need guest-specific credentials. No host SSH, cloud or GPG credentials are forwarded.

Internet access is enabled, including reachability of host/LAN services through yam; this is not an outbound network-isolation policy. Public DNS resolvers (1.1.1.1 and 9.9.9.9) are used; host VPN/private DNS is not inherited. Agents can modify or delete shared code and session data and disclose anything readable, including provider credentials and store contents. They cannot access the backup directory through these mounts. Config is read-only: perform changes on yam, and plugins that insist on writing config files may need additional targeted storage.

Stop and back up

sudo systemctl stop microvm@agents
sudo tar -czf "$HOME/microvm/agents/backups/$(date -u +%Y%m%dT%H%M%SZ).tar.gz" \
  -C "$HOME/microvm/agents" opencode ssh secrets

Stop the VM before copying its database so SQLite's database and WAL files are consistent. Backups contain credentials: keep them private and copy them to a separate disk/off-host backup. No automatic backup schedule is configured. Restore only with the VM stopped, preserving ownership; first back up the current data. The backup command covers sessions, credentials and SSH identity, not the root disk. To preserve the entire guest as well, also back up root.img while stopped. Do not delete ~/microvm/agents when replacing the guest root disk.

Runtime checks on yam

  1. Compare id and file ownership inside/outside the guest; edit a throwaway file under ~/code on each side and check that the other side sees it.
  2. Run nix develop in a project with dependencies already present on yam. Confirm reuse, then try a dependency absent from yam and confirm it installs in the guest without appearing in the host store.
  3. Check findmnt /nix/.ro-store and try creating a file there: it must fail. findmnt ~/.config/opencode must also be read-only. The guest's /nix/var/nix/daemon-socket/socket belongs to its own daemon, not yam's.
  4. Create a session/sign in through the remote client, then stop/start microvm@agents. Reconnect: sessions, provider login and SSH host fingerprint must survive, along with guest-only Nix dependencies and files in the guest home. Also test recovery after host GC removes a dependency used by a guest-built output: the guest should repair that dependency rather than lose its output.
  5. Check DNS and Internet connectivity in the guest. Verify the server requires the generated password and that a different machine cannot reach the guest's SSH/API services, even if it has a route to the private subnet through yam.
  6. Make a stopped-VM backup and test a restore before relying on it for important sessions. Persistence is not protection against disk failure or corruption.

SOPS

Create age identity for your system:

mkdir -p $HOME/.config/sops/age/
read -s SSH_TO_AGE_PASSPHRASE; export SSH_TO_AGE_PASSPHRASE
nix run nixpkgs#ssh-to-age -- \
  -private-key \
  -i $HOME/.ssh/id_ed25519 \
  -o $HOME/.config/sops/age/keys.txt

Display system age recipient (public key) for you system:

age-keygen -y $HOME/.config/sops/age/keys.txt

Get target host age recipient:

cat /etc/ssh/ssh_host_ed25519_key.pub | nix run nixpkgs#ssh-to-age

Configure sops:

nvim .sops.yaml

Edit secret file:

nix run nixpkgs#sops secrets/example.yaml

Investigate

Housekeeping

Remove old generations

When you make changes to your system, Nix creates a new system Generation. All of the changes to the system since the previous generation are stored there. Old generations can add up and will not be removed automatically by default. You can see your generations with:

$ nix-env --list-generations

To keep just your current generation and the two older than it:

$ nix-env --delete-generations +3

To remove all but your current generation:

$ nix-env --delete-generations old

Generation trimmer script

For a smart interactive script which can handle all the normally available profile types across NixOS and be more conservative and safe than the built-in Nix generations deletion commands, see NixOS Generations Trimmer.

Garbage collection

As you work with your system (installs, uninstalls, upgrades), files in the Nix store are not automatically removed, even when no longer needed. Nix instead has a garbage collector which must be run periodically (you could set up, e.g., a cron to do this).

$ nix-collect-garbage

This is safe so long as everything you need is listed in an existing generation or garbage collector root (gcroot).

If you are sure you only need your current generation, this will delete all old generations and then do garbage collection:

$ nix-collect-garbage -d

On NixOS, you can enable a service to automatically do daily garbage collection:

/etc/nixos/configuration.nix

nix.gc.automatic = true;

FAQ

HELP cache is down and I can't remove it!

If a substitute is down and you can't remove it from your configuration since it uses the substitute you can run your build with --option build-use-substitutes false

nix run nixpkgs#nixos-rebuild -- \
  --sudo \
  switch \
  --option build-use-substitutes false \
  --flake .#yam