Setting up `git maintenance register` on NixOS

Trying to set up an equivalent to git maintenance register on NixOS (since running that imperative command literally would generate a service file with hardcoded paths, thereby breaking on every time those paths are invalidated).

This is my attempt, adapted from the service file that git itself would generate:
(Ignore the details of mapListToAttrs, it should be unrelated to the issue at hand.)

  systemd.user = {
    services."git-maintenance@" = {
      description = "Optimize Git repositories data";
      serviceConfig = {
        ExecStart = ''"${getExe config.programs.git.package}" --exec-path="${getBin config.programs.git.package}/bin" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%i'';
      };
    };
    timers =
      mapListToAttrs (OnCalendar: "git-maintenance@${OnCalendar}")
        (OnCalendar: {
          description = "Optimize Git repositories data";
          timerConfig = {
            inherit OnCalendar;
            Persistent = true;
          };
        })
        [
          "hourly"
          "daily"
          "weekly"
        ];
  };

And I have, in my ~/.config/git/config:

[maintenance]
    repo = "/path/to/repo1"
    repo = "/path/to/repo2"
    strategy = "incremental"

However, when I attempt to run (or let the system automatically run) one of the services, e.g. git-maintenance@hourly:

$ systemctl --user status git-maintenance@hourly
× git-maintenance@hourly.service - Optimize Git repositories data
     Loaded: loaded (/etc/systemd/user/git-maintenance@.service; static)
     Active: failed (Result: exit-code) since TIMESTAMP; 1s ago
   Duration: 882ms
 Invocation: ff155269ee424b5a85c69490c8dea89e
TriggeredBy: ● git-maintenance@hourly.timer
    Process: 784921 ExecStart=/nix/store/pxpns5vm111i6j3r3wbygaj99wbrm6h1-git-2.47.0/bin/git for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly (code=exited, status=1/FAILURE)
   Main PID: 784921 (code=exited, status=1/FAILURE)
   Mem peak: 7.2M
        CPU: 100ms

TIMESTAMP HOST systemd[1731]: Started Optimize Git repositories data.
TIMESTAMP HOST git[784929]: error: failed to prefetch remotes
TIMESTAMP HOST git[784929]: error: task 'prefetch' failed
TIMESTAMP HOST systemd[1731]: git-maintenance@hourly.service: Main process exited, code=exited, status=1/FAILURE
TIMESTAMP HOST systemd[1731]: git-maintenance@hourly.service: Failed with result 'exit-code'.

Meanwhile running the command manually works fine:

$ git for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly

$ echo $?                                    
0

How do I get this command to work correctly in a systemd service?

You’ll have to figure out why it fails to prefetch.

My first guess given that operation failing would be networking but it should be in the root networking namespace and therefore function as well as when its ran outside of a systemd service.

I’d try to do basic debugging; whether you are able to curl some internet website, read the relevant git config sections and show the HEAD of some repo on disk from within your systemd service.

If all of that works, you’ll have to strace -f git in your systemd service to see what’s going wrong.