nix-config/modules/services/samba.nix

302 lines
9 KiB
Nix
Raw Normal View History

2024-01-02 15:57:33 +01:00
{
config,
lib,
...
}: {
services.samba-wsdd = {
enable = true; # make shares visible for windows 10 clients
openFirewall = true;
};
2024-01-15 02:13:46 +01:00
age.secrets.resticpasswd = {
generator.script = "alnum";
};
age.secrets.resticHetznerSsh = {
generator.script = "ssh-ed25519";
};
services.restic.backups = {
main = {
user = "root";
timerConfig = {
OnCalendar = "06:00";
Persistent = true;
RandomizedDelaySec = "3h";
};
initialize = true;
passwordFile = config.age.secrets.resticpasswd.path;
hetznerStorageBox = {
enable = true;
inherit (config.secrets.secrets.global.hetzner) mainUser;
inherit (config.secrets.secrets.global.hetzner.users.smb) subUid path;
sshAgeSecret = "resticHetznerSsh";
};
paths = ["/bunker"];
pruneOpts = [
"--keep-daily 10"
"--keep-weekly 7"
"--keep-monthly 12"
"--keep-yearly 75"
];
};
};
2023-11-03 22:59:13 +01:00
services.samba = {
enable = true;
securityType = "user";
openFirewall = true;
enableWinbindd = false;
enableNmbd = false;
2024-01-02 15:57:33 +01:00
extraConfig = lib.concatLines [
''
logging = systemd
log level = 0 auth:2 passdb:2
passdb backend = tdbsam:${config.age.secrets.smbpassdb.path}
server role = standalone
''
# Show the server host name in the printer comment box in print manager
# and next to the IPC connection in net view.
"server string = patricks-tolles-nas"
# Set the NetBIOS name by which the Samba server is known.
2024-01-11 13:09:16 +01:00
"netbios name = patricks-tolles-nas"
2024-01-02 15:57:33 +01:00
# Disable netbios support. We don't need to support browsing since all
# clients hardcode the host and share names.
"disable netbios = yes"
# Deny access to all hosts by default.
"hosts deny = 0.0.0.0/0"
# Allow access to local network
"hosts allow = 192.168.178. 127.0.0.1 10.0.0. localhost"
"guest account = nobody"
"map to guest = bad user"
# Clients should only connect using the latest SMB3 protocol (e.g., on
# clients running Windows 8 and later).
"server min protocol = SMB3_11"
# Require native SMB transport encryption by default.
"server smb encrypt = required"
2024-01-15 21:46:10 +01:00
# Do not map the executable bit to anything
"map archive = no"
"map system = no"
"map hidden = no"
2024-01-02 15:57:33 +01:00
# Disable printer sharing. By default Samba shares printers configured
# using CUPS.
"load printers = no"
"printing = bsd"
"printcap name = /dev/null"
"disable spoolss = yes"
"show add printer wizard = no"
];
shares = let
mkShare = {
name,
user ? "smb",
group ? "smb",
2024-01-15 02:13:46 +01:00
hasBunker ? false,
2024-01-18 00:39:25 +01:00
hasPaperless ? false,
2024-01-15 20:46:53 +01:00
persistRoot ? "/panzer",
2024-01-15 02:13:46 +01:00
}: cfg: let
config =
2024-01-02 15:57:33 +01:00
{
"#persistRoot" = persistRoot;
2024-01-18 00:39:25 +01:00
"#user" = user;
"#group" = group;
2024-01-02 15:57:33 +01:00
"read only" = "no";
"guest ok" = "no";
2024-01-15 21:46:10 +01:00
"create mask" = "0740";
2024-01-02 15:57:33 +01:00
"directory mask" = "0750";
2024-01-18 00:39:25 +01:00
"force user" = user;
"force group" = group;
2024-01-02 15:57:33 +01:00
"valid users" = "${user} @${group}";
"force create mode" = "0660";
"force directory mode" = "0770";
2024-01-10 15:00:59 +01:00
# Might be necessary for windows user to be able to open thing in smb
"acl allow execute always" = "no";
2024-01-02 15:57:33 +01:00
}
// cfg;
2024-01-15 02:13:46 +01:00
in
{
"${name}" =
2024-01-15 20:46:53 +01:00
config
// {"path" = "/media/smb/${name}";};
2024-01-15 02:13:46 +01:00
}
// lib.optionalAttrs hasBunker
{
"${name}-important" =
2024-01-15 20:46:53 +01:00
config
// {
"path" = "/media/smb/${name}-important";
"#persistRoot" = "/bunker";
};
2024-01-18 00:39:25 +01:00
}
// lib.optionalAttrs hasPaperless
{
"${name}-paperless" =
config
// {
"path" = "/media/smb/${name}-paperless";
"#paperless" = true;
"force user" = "paperless";
"force group" = "paperless";
# Empty to prevent imperamence setting a persistence folder
"#persistRoot" = "";
};
2024-01-15 02:13:46 +01:00
};
2024-01-02 15:57:33 +01:00
in
lib.mkMerge [
(mkShare {
name = "ggr-data";
user = "ggr";
group = "ggr";
2024-01-15 02:13:46 +01:00
hasBunker = true;
2024-01-02 15:57:33 +01:00
} {})
(mkShare {
name = "patri";
2024-01-02 15:57:33 +01:00
user = "patrick";
group = "patrick";
2024-01-15 02:13:46 +01:00
hasBunker = true;
2024-01-18 00:39:25 +01:00
hasPaperless = true;
2024-01-02 15:57:33 +01:00
} {})
2024-01-11 13:09:16 +01:00
(mkShare {
name = "helen-data";
user = "helen";
group = "helen";
2024-01-15 02:13:46 +01:00
hasBunker = true;
2024-01-11 13:09:16 +01:00
} {})
(mkShare {
2024-02-13 19:18:05 +01:00
name = "david";
2024-01-11 13:09:16 +01:00
user = "david";
group = "david";
2024-01-15 02:13:46 +01:00
hasBunker = true;
2024-02-13 19:18:05 +01:00
hasPaperless = true;
2024-01-11 13:09:16 +01:00
} {})
(mkShare {
name = "family-data";
user = "family";
group = "family";
} {})
(mkShare {
name = "media";
2024-01-15 02:13:46 +01:00
user = "family";
group = "family";
persistRoot = "/renaultft";
}
2024-01-02 15:57:33 +01:00
{
"read only" = "yes";
2024-01-11 13:09:16 +01:00
"write list" = "@family";
2024-01-02 15:57:33 +01:00
})
];
2023-11-03 22:59:13 +01:00
};
2024-01-11 13:09:16 +01:00
# to get this file start a smbd, add users using 'smbpasswd -a <user>'
2023-11-05 15:54:29 +01:00
# then export the database using 'pdbedit -e tdbsam:<location>'
age.secrets.smbpassdb = {
rekeyFile = ../../secrets/smbpassdb.tdb.age;
2023-11-03 22:59:13 +01:00
};
2024-01-02 15:57:33 +01:00
users = let
users = lib.unique (lib.mapAttrsToList (_: val: val."force user") config.services.samba.shares);
groups = lib.unique (users ++ (lib.mapAttrsToList (_: val: val."force group") config.services.samba.shares));
in {
2024-01-18 00:39:25 +01:00
users = lib.mkMerge ((lib.flip map users (user: {
${user} = {
isNormalUser = true;
home = "/var/empty";
createHome = false;
useDefaultShell = false;
autoSubUidGidRange = false;
group = "${user}";
};
}))
++ [
{paperless.isNormalUser = lib.mkForce false;}
]);
2024-01-15 02:13:46 +01:00
groups = lib.mkMerge ((lib.flip map groups (group: {
${group} = {
};
}))
++ [{family.members = ["patrick" "david" "helen" "ggr"];}]);
2023-11-03 22:59:13 +01:00
};
2024-01-02 15:57:33 +01:00
2024-01-18 00:39:25 +01:00
fileSystems = lib.mkMerge (lib.flip lib.mapAttrsToList config.services.samba.shares (_: v:
lib.optionalAttrs ((v ? "#paperless") && v."#paperless") {
"${v.path}/consume" = {
fsType = "none";
options = ["bind"];
device = "/paperless/consume/${v."#user"}";
};
"${v.path}/media/archive" = {
2024-01-18 00:39:25 +01:00
fsType = "none ";
options = ["bind" "ro"];
device = "/paperless/media/documents/archive/${v."#user"}";
};
"${v.path}/media/originals" = {
fsType = "none ";
options = ["bind" "ro"];
device = "/paperless/media/documents/originals/${v."#user"}";
2024-01-18 00:39:25 +01:00
};
}));
systemd.tmpfiles.settings = lib.mkMerge (lib.flip lib.mapAttrsToList config.services.samba.shares (_: v:
lib.optionalAttrs ((v ? "#paperless") && v."#paperless") {
2024-01-19 22:33:03 +01:00
"10-smb-paperless"."/paperless/consume/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-01-18 00:39:25 +01:00
"10-smb-paperless"."/paperless/consume/${v."#user"}".d = {
user = "paperless";
group = "paperless";
mode = "0770";
2024-01-18 00:39:25 +01:00
};
2024-01-19 22:33:03 +01:00
"10-smb-paperless"."/paperless/media/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
"10-smb-paperless"."/paperless/media/documents/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
"10-smb-paperless"."/paperless/media/documents/archive/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
"10-smb-paperless"."/paperless/media/documents/archive/${v."#user"}".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-01-19 22:33:03 +01:00
"10-smb-paperless"."/paperless/media/documents/archive/${v."#user"}/.keep".f = {
user = "paperless";
group = "paperless";
mode = "0660";
};
"10-smb-paperless"."/paperless/media/documents/originals/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
"10-smb-paperless"."/paperless/media/documents/originals/${v."#user"}".d = {
2024-01-18 00:39:25 +01:00
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-01-19 22:33:03 +01:00
"10-smb-paperless"."/paperless/media/documents/originals/${v."#user"}/.keep".f = {
user = "paperless";
group = "paperless";
mode = "0660";
};
2024-01-18 00:39:25 +01:00
}));
environment.persistence = lib.mkMerge (lib.flip lib.mapAttrsToList config.services.samba.shares (_: v:
lib.optionalAttrs ((v ? "#persistRoot") && (v."#persistRoot" != "")) {
${v."#persistRoot"}.directories = [
{
directory = "${v.path}";
user = "${v."force user"}";
group = "${v."force group"}";
mode = "0770";
}
];
}));
2023-11-03 22:59:13 +01:00
}