Personal-use NixOS configuration
1{ lib }:
2
3# targets:
4# [ { name = str?; path = str; userName = str; groupName = str?; } ];
5targets:
6
7{ config, ... }:
8
9let
10 extraNfsExports = ''
11 /export *(fsid=0,ro,insecure)
12 '';
13
14 mkShare =
15 target:
16 let
17 directories = lib.filter (x: x != "") (lib.splitString "/" target.path);
18
19 exportPath = "/export/${target.name or (lib.last directories)}";
20
21 mountName = builtins.elemAt directories 1;
22 isZfsPool = lib.elem mountName config.boot.zfs.extraPools;
23
24 user = config.users.users.${target.userName};
25 group = config.users.groups.${target.groupName or user.group};
26
27 userId = toString user.uid;
28 groupId = toString (group.gid or user.gid);
29 in
30 {
31 inherit exportPath;
32
33 fileSystemMount = {
34 device = target.path;
35 fsType = "none";
36
37 depends = lib.optionals isZfsPool [ "/mnt/${mountName}" ];
38 options = [ "bind" ] ++ lib.optionals isZfsPool [ "x-systemd.requires=zfs-mount.service" ];
39 };
40
41 nfsExport = ''
42 "${exportPath}" *(rw,insecure,async,no_subtree_check,nohide,all_squash,anonuid=${userId},anongid=${groupId})
43 '';
44 };
45
46 shares = map mkShare targets;
47in
48{
49 fileSystems = builtins.listToAttrs (
50 map (share: {
51 name = share.exportPath;
52 value = share.fileSystemMount;
53 }) shares
54 );
55
56 services.nfs.server.exports =
57 extraNfsExports + lib.concatStringsSep "\n" (map (share: share.nfsExport) shares);
58}