How to use `services.self-deploy`?


I would like to use the services.self-deploy on a server.
I find the concept of this service really interesting.

However, it is not clear to me how I should configure it.

Current configuration:

services.self-deploy = {
    enable = true;

    startAt = "hourly";

    repository = "";
    nixFile = "/nix/${}.nix";
    nixAttribute = "system";
    sshKeyFile = "${config.users.users.gaetan.home}/.ssh/rsa_server";

Generated service (/etc/systemd/system/self-deploy.service):


Environment="GIT_SSH_COMMAND=/nix/store/rqffx2f2b2vyzbssjh2fbbmnx3p15c12-openssh-9.0p1/bin/ssh -i '/home/gaetan/.ssh/rsa_server'"



The service gets created correctly, it gets started correctly, but the execution fails.

[gaetan@backup:~/server]$ systemctl status self-deploy                                                                                                      
× self-deploy.service
     Loaded: loaded (/etc/systemd/system/self-deploy.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2022-08-01 17:05:41 CEST; 44s ago
TriggeredBy: ● self-deploy.timer
    Process: 18233 ExecStart=/nix/store/7lcmdzmkxdqbc078xi12pa3dj1q4gkh6-unit-script-self-deploy-start/bin/self-deploy-start (code=exited, status=1/FAILURE)
   Main PID: 18233 (code=exited, status=1/FAILURE)
         IP: 4.9K in, 5.2K out
        CPU: 52ms

Aug 01 17:05:39 backup systemd[1]: Started self-deploy.service.
Aug 01 17:05:41 backup self-deploy-start[18234]: From
Aug 01 17:05:41 backup self-deploy-start[18234]:  * branch            master     -> FETCH_HEAD
Aug 01 17:05:41 backup self-deploy-start[18240]: HEAD is now at bbd0087 test self-deploy
Aug 01 17:05:41 backup self-deploy-start[18241]: error: anonymous function at /var/lib/nixos-self-deploy/repo/nix/backup.nix:1:1 called without required argument 'config'
Aug 01 17:05:41 backup systemd[1]: self-deploy.service: Main process exited, code=exited, status=1/FAILURE
Aug 01 17:05:41 backup systemd[1]: self-deploy.service: Failed with result 'exit-code'.
Aug 01 17:05:41 backup systemd[1]: self-deploy.service: Consumed 52ms CPU time, received 4.8K IP traffic, sent 5.2K IP traffic.

Here is the generated script (/nix/store/7lcmdzmkxdqbc078xi12pa3dj1q4gkh6-unit-script-self-deploy-start/bin/self-deploy-start):

set -e
if [ ! -e /var/lib/nixos-self-deploy/repo ]; then
  mkdir --parents /var/lib/nixos-self-deploy/repo
  git init /var/lib/nixos-self-deploy/repo

git -C /var/lib/nixos-self-deploy/repo fetch '' 'master'

git -C /var/lib/nixos-self-deploy/repo checkout FETCH_HEAD

nix-build '--attr' 'system' '--out-link' '/var/lib/nixos-self-deploy/system' '/var/lib/nixos-self-deploy/repo/nix/backup.nix'

nix-env --profile /nix/var/nix/profiles/system --set /var/lib/nixos-self-deploy/system

/var/lib/nixos-self-deploy/system/bin/switch-to-configuration switch

rm /var/lib/nixos-self-deploy/system

git -C /var/lib/nixos-self-deploy/repo gc --prune=all

The following line is posing problem:
nix-build '--attr' 'system' '--out-link' '/var/lib/nixos-self-deploy/system' '/var/lib/nixos-self-deploy/repo/nix/backup.nix'

Is it a problem of configuration ?
Is the generated nix-build command correct ?

Thank you very much in advance for your help !


It looks like the issue is with your configuration to me – more specifically, it looks like the file you’re referencing is what would normally be in your configuration.nix file exactly, aka a function. I’ve been meaning to document the self-deploy service for some time, but it requires the configuration be a “system closure”, which is created by using the methodology Gabriella Gonzalez describes here: Haskell for all: NixOS in production

The article is a bit out of date in a handful of regards, but the actual methodology of creating a buildable system closure is not. The parts you need to look at are “building the closure” (specifically the syntax used to create the closure, self-deploy does the building) and “pinning nixpkgs” (likely something you’d like to do when using self-deploy), self-deploy handles all of the other parts.

Ok, thank you for this explanation !

Indeed, the file I was referencing was simply a renamed configuration.nix.
I wrote a closure as detailed in the guide, however, I initially wanted something yo “automatically update” my system. In other words, pull the repository containing my configuration and nixos-rebuild switch it.
Combined to the use of system.autoUpgrade, I was hoping to get an “autonomous system” that keeps itself up to date both regarding the configuration on my git and the nixpkgs 22.05 channel.

Would it be possible ? Is it the proper approach ?

That certainly could be done, yes. You would want to ignore the instructions for pinning nixpkgs and instead use the nixpkgs channel, then leave autoUpgrade in charge of managing your nixpkgs channel.

1 Like

In case it’s useful, Cachix Deploy Public Beta - Cachix Blog addresses a lot of the design flaws related to self deploy while providing a clean interface.