summaryrefslogtreecommitdiff
path: root/machines/kusanagi/default.nix
blob: 867a5eb7191a899903b822a978dcbf4cc1d9ed80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# kusanagi — AMD workstation. Everything here is specific to this machine:
# ROCm/GPU, the Focusrite Scarlett audio interface, optical-disc archiving, the
# NAS mounts, and the Kavita/Immich/Ollama servers.
{ config, pkgs, ... }:

{
  imports = [
    ../../profiles/desktop.nix
    ./desktop.nix
    ../../profiles/dev.nix
    ../../profiles/apps.nix
    ../../profiles/sandbox.nix
    ./hardware-configuration.nix
  ];

  # Run coding agents under the isolated `yokai` sandbox user; henz owns the
  # canonical repos in ~. See profiles/sandbox.nix for the full workflow.
  sandbox.owner = "henz";

  networking.hostName = "kusanagi";
  lanDiscovery.interface = "wlp8s0";

  # Ollama is served on the Wi-Fi LAN only. Tailscale remains governed by ACLs
  # plus the host firewall because tailscale0 is not a trusted interface.
  networking.firewall.interfaces.${config.lanDiscovery.interface}.allowedTCPPorts = [ 11434 ];

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  system.stateVersion = "24.11"; # Did you read the comment?

  powerManagement.cpuFreqGovernor = "performance";

  # NOTE: idle sleep intentionally not configured. Suspend-to-RAM bounced awake
  # within ~5 s on this hardware (spurious S3 wakeup) and there's no swap to
  # hibernate into. Deferred to a future disko rebuild (real swap/encryption).

  boot.binfmt.emulatedSystems = [ "aarch64-linux" ];

  # --- GPU (ROCm) ---
  nixpkgs.config.rocmSupport = true;
  hardware.graphics.extraPackages = with pkgs; [
    rocmPackages.clr.icd
  ];

  # --- Optical disc drives / archiving ---
  boot.kernelModules = [ "sg" ]; # for disc drive

  # For disc drives; plus systemd-tag the GPU compute nodes (kfd/renderD*) so
  # ollama.service can order after them — char devices get no systemd device
  # unit unless tagged.
  services.udev.extraRules = ''
    KERNEL=="sr[0-9]*", GROUP="cdrom", MODE="0660"
    KERNEL=="sg[0-9]*", GROUP="sg", MODE="0660"
    SUBSYSTEM=="kfd", TAG+="systemd"
    SUBSYSTEM=="drm", KERNEL=="renderD*", TAG+="systemd"
  '';

  services.pcscd.enable = true;

  # abcde: patch its musicbrainz tool to accept discs with 4 args.
  nixpkgs.overlays = [
    (self: super: {
      abcde = super.abcde.overrideAttrs (oldAttrs: {
        # We use postInstall to modify the script after it has been copied to the output directory
        postInstall = (oldAttrs.postInstall or "") + ''
          # Patch 1: Allow GetOptions to accept 4 arguments (instead of requiring 5+)
          sed -i 's/discinfo=i{5,}/discinfo=i{4,}/g' $out/bin/abcde-musicbrainz-tool

          # Patch 2: Lower the logic check from 5 to 4
          sed -i 's/$#discinfo < 5/$#discinfo < 4/g' $out/bin/abcde-musicbrainz-tool
        '';
      });
    })
  ];

  # --- Scanner ---
  hardware.sane.enable = true;

  # --- Audio interface (Focusrite Scarlett 18i20) ---
  # Add this for better pro audio support
  services.pipewire.extraConfig.pipewire."10-low-latency" = {
    "context.properties" = {
      "default.clock.rate" = 48000;
      "default.clock.quantum" = 256;
      "default.clock.min-quantum" = 256;
      "default.clock.max-quantum" = 2048;
    };
  };

  # Pin the Scarlett 18i20 to the raw "Pro Audio" profile.
  # The 26.05 pipewire/wireplumber update began preferring the UCM "HiFi"
  # profile, which splits the interface into Direct1/Line5/SPDIF1/... sinks and
  # broke monitoring. This priority rule forces the multichannel Pro Audio
  # profile statelessly on every boot (and survives future UCM renames).
  services.pipewire.wireplumber.extraConfig."51-scarlett-pro-audio" = {
    "device.profile.priority.rules" = [
      {
        matches = [
          {
            "device.name" = "alsa_card.usb-Focusrite_Scarlett_18i20_USB_P9DCR6Y378D1E6-00";
          }
        ];
        actions = {
          update-props = {
            priorities = [ "pro-audio" ];
          };
        };
      }
    ];
  };

  # Real-time audio optimizations
  security.pam.loginLimits = [
    {
      domain = "@audio";
      item = "memlock";
      type = "-";
      value = "unlimited";
    }
    {
      domain = "@audio";
      item = "rtprio";
      type = "-";
      value = "99";
    }
    {
      domain = "@audio";
      item = "nofile";
      type = "soft";
      value = "99999";
    }
    {
      domain = "@audio";
      item = "nofile";
      type = "hard";
      value = "99999";
    }
  ];

  # Kernel optimizations for audio
  boot.kernelParams = [
    "threadirqs"
    "preempt=full" # if using a PREEMPT kernel
    # Add USB audio optimizations
    "usbcore.usbfs_memory_mb=1024"
  ];

  # --- MPD (music) ---
  services.mpd.enable = false;

  systemd.user.services.mpd = {
    description = "Music Player Daemon";
    after = [
      "network.target"
      "sound.target"
    ];
    wantedBy = [ "default.target" ];
    serviceConfig = {
      ExecStart = "${pkgs.mpd}/bin/mpd --no-daemon";
      Type = "notify";
      LimitRTPRIO = 50;
      LimitRTTIME = "infinity";
    };
  };

  # --- NAS (NFS) ---
  boot.supportedFilesystems = [ "nfs" ];

  services.nfs.server.enable = false;
  services.rpcbind.enable = true;

  services.nfs.idmapd.settings = {
    General = {
      Domain = "kusanagi"; # or your actual domain name
    };
  };

  # NAS shares. Use the mDNS-resolvable name (bare "milgrim" doesn't resolve)
  # and automount on first access so a boot-time DNS/network race can't leave
  # them unmounted (which silently breaks MPD, whose DB points at these paths).
  fileSystems."/mnt/milgrim-share" = {
    device = "milgrim.local:/volume1/share";
    fsType = "nfs";
    options = [
      "nfsvers=4"
      "rsize=8192"
      "wsize=8192"
      "_netdev"
      "noauto"
      "x-systemd.automount"
      "x-systemd.idle-timeout=600"
      "x-systemd.mount-timeout=10s"
    ];
  };

  fileSystems."/mnt/milgrim-public" = {
    device = "milgrim.local:/volume1/public";
    fsType = "nfs";
    options = [
      "nfsvers=4"
      "rsize=8192"
      "wsize=8192"
      "_netdev"
      "noauto"
      "x-systemd.automount"
      "x-systemd.idle-timeout=600"
      "x-systemd.mount-timeout=10s"
    ];
  };

  # --- Servers ---
  services.kavita = {
    enable = true;
    tokenKeyFile = "/var/lib/kavita/token-key";
    settings.Port = 5000;
  };

  systemd.tmpfiles.rules = [
    "d /srv/library 2750 henz kavita - -"
  ];

  services.ollama = {
    enable = true;
    package = pkgs.ollama-rocm;
    host = "0.0.0.0";
    environmentVariables = {
      # No HIP_VISIBLE_DEVICES / HSA_OVERRIDE_GFX_VERSION: the 7900 XT is
      # native gfx1100 and discovery excludes the unsupported Raphael iGPU
      # (gfx1036) on its own; ollama warns that overriding visible devices can
      # itself break discovery. The past "runs 100% on CPU" incidents were the
      # boot-order race handled below, not a gfx-target mismatch.
      # KV-cache quantization (below) is a no-op without flash attention; ollama
      # falls back to f16 KV cache, inflating VRAM use and forcing CPU offload.
      OLLAMA_FLASH_ATTENTION = "1";
      OLLAMA_CONTEXT_LENGTH = "65536";
      OLLAMA_KV_CACHE_TYPE = "q8_0";
      # Keep the model resident between agent turns; the default 5-minute
      # keep-alive forces a multi-second reload after any pause.
      OLLAMA_KEEP_ALIVE = "1h";
    };
  };

  # ollama probes GPUs once at startup. At boot it raced amdgpu bring-up
  # (started between /dev/kfd and the render node appearing) and permanently
  # fell back to CPU. Order it after the tagged compute nodes (udev rules
  # above) so discovery only runs once the card is really there.
  systemd.services.ollama = {
    wants = [
      "dev-kfd.device"
      "dev-dri-renderD128.device"
    ];
    after = [
      "dev-kfd.device"
      "dev-dri-renderD128.device"
    ];
  };

  services.immich = {
    enable = true;
    machine-learning.enable = false;
  };

  # --- discidurl / haunt tooling (needs the per-machine overlays in flake.nix) ---
  environment.systemPackages =
    let
      # open the MusicBrainz submission URL for the disc in the drive
      discid = pkgs.writeShellApplication {
        name = "discid";
        runtimeInputs = [
          pkgs.discidurl
          pkgs.xdg-utils
        ];
        text = "discidurl | xargs -r xdg-open";
      };
    in
    with pkgs;
    [
      discidurl
      discid
      haunt
    ];

  # --- User account (henz) ---
  # (Desktop groups audio/lp/input come from profiles/desktop.nix.)
  users.users.henz.isNormalUser = true;
  users.users.henz.description = "Henry Webster";
  users.users.henz.extraGroups = [
    # base
    "networkmanager"
    "wheel"
    # kusanagi hardware
    "cdrom"
    "optical"
    "sg"
    "scanner"
  ];

  nix.settings.trusted-users = [
    "root"
    "henz"
  ];

  users.users.henz.packages = with pkgs; [
    # base CLI
    git
    neovim
    tmux
    btop
    ripgrep
    unzip
    wl-clipboard
    bc
    psmisc
    usbutils
    stow
    nushell

    # kusanagi
    blender
    rocmPackages.rpp
    rocmPackages.hipcc
    rocmPackages.rocminfo
    ardour
    davinci-resolve-studio
    calf
    alsa-utils
    alsa-tools
    pipewire.jack
    nfs-utils

    # music
    mpd
    mpc
    ncmpcpp

    # for archiving
    #makemkv
    abcde
    libaacs
    libbdplus
    libdvdcss
    mkvtoolnix
    dvdbackup
    chromaprint
    picard
    flac

    # scanners
    simple-scan

    (pkgs.makeDesktopItem {
      name = "PICO-8";
      desktopName = "PICO-8";
      exec = "pico8";
      icon = "pico8";
      comment = "Fantasy console for making, sharing and playing tiny games";
      categories = [ "Game" ];
    })
  ];
}