NixOS 25.11: I tried to create a systemd oneshot unit with an associated timer. My unit is more complex, but even this simplified version, put directly in configuration.nix, doesn’t seem to install any unit files (systemctl list-unit-files doesn’t show them):
systemd.services.my-task = {
enable = true;
description = "My periodic task";
path = [ pkgs.coreutils pkgs.bash ]; # ensure required binaries are on PATH
serviceConfig = {
Type = "oneshot";
ExecStart = "${myScript}";
User = "root";
};
wantedBy = [ "multi-user.target" ]; # optional; ensures it can be started manually or on boot
};
systemd.timers.my-task = {
enable = true;
description = "Timer for my periodic task";
wantedBy = [ "timers.target" ];
timerConfig = {
OnUnitActiveSec = "5min";
Unit = "my-task.service";
AccuracySec = "10s";
};
};
Seems like the issue was due to a bad activation script. Specifically
the following:
system.activationScripts.check-default-profile-empty = ''
set -eu -o pipefail
nix_profile_list=$(${pkgs.nix}/bin/nix profile list --profile /nix/var/nix/profiles/default 2>/dev/null)
# Exit 0 if default profile empty; exit 1 (error) otherwise
if printf '%s' "$nix_profile_list" | grep -q "Store paths:"; then
printf "ERROR: /nix/var/nix/profiles/default is non-empty — manual packages installed:\n%s\n" \
"$nix_profile_list" >&2
exit 1
fi
exit 0
'';
Turns out that grep might not be included on the default PATH. But
somehow it was still activating a profile (but only the most recent
working profile, not the one I wanted). I need to include the full path
to grep:
system.activationScripts.check-default-profile-empty = ''
set -eu -o pipefail
nix_profile_list=$(${pkgs.nix}/bin/nix profile list --profile /nix/var/nix/profiles/default 2>/dev/null)
if printf '%s' "$nix_profile_list" | ${pkgs.gnugrep}/bin/grep -q "Store paths:"; then
printf "ERROR: /nix/var/nix/profiles/default is non-empty — manual packages installed:\n%s\n" \
"$nix_profile_list" >&2
exit 1
fi
'';
After making that change, the new systemd unit is successfully being installed into the active system configuration.