| 1 | { config, lib, pkgs, ... }: |
| 2 | let |
| 3 | root = "/srv/knife-linux"; |
| 4 | uid = toString config.users.users.clo.uid; |
| 5 | gid = toString config.users.groups.chloe.gid; |
| 6 | engine = pkgs.writeText "knife-linux-containers.conf" '' |
| 7 | [engine] |
| 8 | tmp_dir = "/run/knife-linux/libpod" |
| 9 | lock_type = "file" |
| 10 | ''; |
| 11 | # A second rootful store must not share Nomad's lock segment or state directory. |
| 12 | podman = pkgs.writeShellScriptBin "knife-linux-podman" '' |
| 13 | export CONTAINERS_CONF_OVERRIDE=${engine} |
| 14 | exec ${pkgs.podman}/bin/podman --root=${root}/containers/storage --runroot=/run/knife-linux/containers "$@" |
| 15 | ''; |
| 16 | run = "${podman}/bin/knife-linux-podman"; |
| 17 | cpus = "8-15,24-31"; |
| 18 | win11Gaming = builtins.fromJSON (builtins.readFile ../config/win11-gaming.json); |
| 19 | checkWork = pkgs.writeShellScript "check-knife-work" '' |
| 20 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/work)" = work |
| 21 | ''; |
| 22 | in |
| 23 | { |
| 24 | environment.systemPackages = [ podman ]; |
| 25 | |
| 26 | systemd.services.knife-linux-storage = { |
| 27 | requires = [ "zfs-import-work.service" "dev-tpmrm0.device" ]; |
| 28 | after = [ "zfs-import-work.service" "zfs-mount.service" "dev-tpmrm0.device" ]; |
| 29 | before = [ "knife-linux.service" ]; |
| 30 | serviceConfig = { |
| 31 | Type = "oneshot"; |
| 32 | RemainAfterExit = true; |
| 33 | LoadCredentialEncrypted = "zfs-clover:/etc/credstore.encrypted/zfs-clover"; |
| 34 | }; |
| 35 | script = '' |
| 36 | test "$(${pkgs.zfs}/bin/zfs get -H -o value encryptionroot work/knife-linux)" = work/knife-linux |
| 37 | test "$(${pkgs.zfs}/bin/zfs get -H -o value mountpoint work/knife-linux)" = ${root} |
| 38 | if test "$(${pkgs.zfs}/bin/zfs get -H -o value keystatus work/knife-linux)" = unavailable; then |
| 39 | ${pkgs.zfs}/bin/zfs load-key -L "file://$CREDENTIALS_DIRECTORY/zfs-clover" work/knife-linux |
| 40 | fi |
| 41 | if test "$(${pkgs.zfs}/bin/zfs get -H -o value mounted work/knife-linux)" != yes; then |
| 42 | ${pkgs.zfs}/bin/zfs mount work/knife-linux |
| 43 | fi |
| 44 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint ${root})" = work/knife-linux |
| 45 | ''; |
| 46 | }; |
| 47 | |
| 48 | systemd.services.knife-linux = { |
| 49 | description = "The Knife Linux agent container"; |
| 50 | wantedBy = [ "multi-user.target" ]; |
| 51 | wants = [ "network-online.target" ]; |
| 52 | after = [ "network-online.target" "knife-linux-storage.service" ]; |
| 53 | requires = [ "knife-linux-storage.service" ]; |
| 54 | # Restarting stops every running agent, so changes wait for a manual restart. |
| 55 | restartIfChanged = false; |
| 56 | serviceConfig = { |
| 57 | Type = "notify"; |
| 58 | NotifyAccess = "all"; |
| 59 | ExecStartPre = [ |
| 60 | (pkgs.writeShellScript "check-knife-root" '' |
| 61 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint ${root})" = work/knife-linux |
| 62 | '') |
| 63 | checkWork |
| 64 | "${run} build --quiet --build-arg=UID=${uid} --build-arg=GID=${gid} --tag=localhost/knife-linux --file=${./knife-linux.Containerfile} /var/empty" |
| 65 | ]; |
| 66 | ExecStart = lib.concatStringsSep " " [ |
| 67 | run "run --rm --replace --name=knife-linux --pull=never" |
| 68 | "--sdnotify=conmon --cgroups=no-conmon --log-driver=journald --init --hostname=knife-linux" |
| 69 | "--user=${uid}:${gid} --umask=0077 --cap-drop=ALL --security-opt=no-new-privileges" |
| 70 | "--memory=72g --pids-limit=32768 --shm-size=2g --network=podman" |
| 71 | "--volume=${root}/home:/home/clo" |
| 72 | "--volume=/srv/work:/work" |
| 73 | "--volume=/opt/studio-project-store/current/bin/jj-real:/usr/local/bin/jj:ro" |
| 74 | "--device=/dev/kvm" |
| 75 | "localhost/knife-linux" |
| 76 | ]; |
| 77 | # Agents get 8 cores to themselves, win11-gaming 5 and the host keeps three. A partition |
| 78 | # rather than a CPU quota: an exhausted quota freezes the Knife server with the builds. |
| 79 | ExecStartPost = "-" + pkgs.writeShellScript "knife-linux-cpus" '' |
| 80 | set -e |
| 81 | scope=/sys/fs/cgroup$(${run} inspect --format '{{.State.CgroupPath}}' knife-linux) |
| 82 | echo ${cpus},${win11Gaming.cpus} > /sys/fs/cgroup/machine.slice/cpuset.cpus.exclusive |
| 83 | echo ${cpus} > $scope/cpuset.cpus.exclusive |
| 84 | echo ${cpus} > $scope/cpuset.cpus |
| 85 | echo root > $scope/cpuset.cpus.partition |
| 86 | ''; |
| 87 | ExecStop = "${run} stop --ignore --time=90 knife-linux"; |
| 88 | ExecStopPost = "${run} rm --ignore --force knife-linux"; |
| 89 | KillMode = "mixed"; |
| 90 | TimeoutStartSec = "30min"; |
| 91 | TimeoutStopSec = 120; |
| 92 | Restart = "always"; |
| 93 | RestartSec = 5; |
| 94 | }; |
| 95 | }; |
| 96 | |
| 97 | systemd.services.knife-linux-oom = { |
| 98 | description = "Protect Knife harnesses and prioritize the server over agent work"; |
| 99 | wantedBy = [ "knife-linux.service" ]; |
| 100 | bindsTo = [ "knife-linux.service" ]; |
| 101 | after = [ "knife-linux.service" ]; |
| 102 | serviceConfig = { |
| 103 | ExecStart = "${pkgs.python3}/bin/python3 ${../tools/knife-linux-oom.py} ${run}"; |
| 104 | Restart = "always"; |
| 105 | RestartSec = 1; |
| 106 | }; |
| 107 | }; |
| 108 | } |