Automatic NixOS upgrades with a CI-managed flake lock and deploy keys

Posted on Sat 28 November 2026 in NixOS

Across my NixOS machines, I wanted one place to manage flake.lock for automatic upgrades. Daily patching has become the standard, but updating flake.lock on every machine ended up in Git conflicts and left each host on a different revision.

This is why I let my CI (currently GitHub Actions) manage flake.lock and give every host a read-only deploy key, so all machines upgrade to the same locked revision on a schedule. Also, the setup is cheap and easy: as of writing this, GitHub Actions includes 2,000 free minutes per month for private repositories, and updating flake.lock takes about a minute per day.

It has three parts: a GitHub Action that updates and pushes the lock, a NixOS module that runs system.autoUpgrade, and one deploy key per host.

Part 1: Update the lock in CI

The workflow runs once a day and on demand. It uses the fantastic DeterminateSystems/nix-installer-action to install Nix, then updates every flake input and pushes flake.lock only if the file changed.

name: update-flake-lock

on:
  schedule:
    - cron: "17 13 * * *"
      timezone: "UTC"
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: update-flake-lock
  cancel-in-progress: false

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7.0.1
        with:
          ref: main
      - uses: DeterminateSystems/nix-installer-action@v23
      - name: Update flake.lock and push
        run: |
          nix flake update
          if git diff --quiet -- flake.lock; then
            echo "flake.lock already up to date"
            exit 0
          fi
          git config user.name "github-actions[bot]"
          git config user.email "<bot-id>+github-actions[bot]@users.noreply.github.com"
          git add flake.lock
          git commit -m "chore(repo): update flake.lock"
          git push origin main

permissions: contents: write lets the workflow token push. The concurrency group stops two update jobs from overlapping. If the lock did not change, the job exits before it creates a commit.

<bot-id> is the numeric GitHub user ID. The built-in github-actions[bot] account uses 41898282, and GitHub's noreply format is <user-id>+<username>@users.noreply.github.com.

In my case, the schedule runs at 13:17 UTC, well before the hosts upgrade at 04:00 local time. If branch protection blocks the push, open a pull request instead, or allow the workflow token to bypass branch protection.

Part 2: Configure autoUpgrade

For a private repository, the host needs read access. NixOS fetches the flake from main and builds the host attribute with the lock that CI committed.

{ config, pkgs, ... }:

{
  system.autoUpgrade = {
    enable = true;
    flake = "git+ssh://git@github.com/<owner>/<repo>.git?ref=main#${config.networking.hostName}";
    flags = [ "--no-write-lock-file" "-L" ];
    upgrade = false;
    dates = "04:00";
    randomizedDelaySec = "45min";
    persistent = true;
    allowReboot = false;
  };

  systemd.services.nixos-upgrade = {
    environment.GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -F /dev/null -i /etc/secrets/nix-config/deploy_key -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/etc/ssh/ssh_known_hosts";

    preStart = ''
      test -r /etc/secrets/nix-config/deploy_key || {
        echo "Missing /etc/secrets/nix-config/deploy_key"
        exit 1
      }
    '';
  };

  programs.ssh.knownHosts.github = {
    hostNames = [ "github.com" ];
    publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl";
  };
}

upgrade = false and --no-write-lock-file keep the CI-managed lock authoritative. Without them, nixos-rebuild can resolve newer inputs and each host drifts again. -L writes build logs to the journal.

GIT_SSH_COMMAND ignores the root SSH configuration, uses only the deploy key, and fails instead of prompting. programs.ssh.knownHosts pins GitHub's host key, so the fetch also fails if GitHub presents a different key. The preStart check turns a missing key into a clear error before Nix starts.

dates sets the time, randomizedDelaySec spreads the hosts out, and persistent runs a missed upgrade after boot. allowReboot = false switches the system but leaves kernel and initrd changes for a manual reboot. Set it to true only where automatic reboots are acceptable.

Part 3: Create a deploy key per host

Generate the key on the host. The service runs unattended, so the key has no passphrase. Root-only permissions protect the key.

sudo install -d -m 700 /etc/secrets/nix-config
sudo ssh-keygen -t ed25519 -N "" -C "nix-config@$(hostname)" -f /etc/secrets/nix-config/deploy_key
sudo chmod 600 /etc/secrets/nix-config/deploy_key
sudo cat /etc/secrets/nix-config/deploy_key.pub

Add the public key as a read-only deploy key in the repository settings. Use a title that identifies the host, for example nixos-web-01, and leave write access disabled. Repeat this for every host.

Keep the private key out of the Nix store and the repository. If you use sops-nix or agenix, point GIT_SSH_COMMAND at the decrypted path instead of /etc/secrets.

Check the timer and run the service once:

systemctl list-timers nixos-upgrade.timer
sudo systemctl start nixos-upgrade
journalctl -u nixos-upgrade -f

The service fetches main, builds the host configuration, and switches to the new generation. A failed fetch or build leaves the current generation running.

Bonus: Enable automatic garbage collection and limit boot entries

Every upgrade adds a system generation, and each generation keeps its store paths alive. Add this to the same module so old generations do not fill the disk:

nix.gc = {
  automatic = true;
  dates = "daily";
};

systemd.services.nix-gc.preStart = ''
  ${config.nix.package.out}/bin/nix-env --delete-generations --profile /nix/var/nix/profiles/system +2
'';

nix.optimise.automatic = true;
boot.loader.systemd-boot.configurationLimit = 5;

The preStart hook keeps the two newest system generations, so garbage collection can free the rest. nix.optimise.automatic runs the store optimizer automatically, and boot.loader.systemd-boot.configurationLimit keeps five entries in the boot menu. If your hosts use GRUB, set boot.loader.grub.configurationLimit instead.

Caveats

A read-only deploy key can read the whole repository. One key per host limits the damage from a compromised machine.

nix flake update updates every input. If you want less churn, update only nixpkgs, or open a pull request and review the lock diff before merging.

Further information

This post is an opinionated version of the NixOS manual article on system.autoUpgrade. Check the reference for all options.