nix-config/config/services/samba.nix

358 lines
10 KiB
Nix
Raw Permalink Normal View History

2024-12-20 20:40:27 +01:00
{
config,
lib,
globals,
...
}:
2024-10-10 19:11:21 +02:00
let
shares = lib.removeAttrs config.services.samba.settings [ "global" ];
in
2024-01-02 15:57:33 +01:00
{
2025-01-07 21:23:06 +01:00
# allow direct access to shares
networking.nftables.firewall.zones.untrusted.interfaces = [ "mv-home" ];
services.samba-wsdd = {
enable = true; # make shares visible for windows 10 clients
openFirewall = true;
};
2024-03-28 10:52:07 +01:00
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;
2024-12-20 20:40:27 +01:00
inherit (globals.hetzner) mainUser;
inherit (globals.hetzner.users.smb) subUid path;
2024-01-15 02:13:46 +01:00
sshAgeSecret = "resticHetznerSsh";
};
2024-07-26 22:12:48 +02:00
paths = [ "/bunker" ];
2024-08-08 20:08:01 +02:00
##pruneOpts = [
## "--keep-daily 10"
## "--keep-weekly 7"
## "--keep-monthly 12"
## "--keep-yearly 75"
##];
2024-01-15 02:13:46 +01:00
};
};
2023-11-03 22:59:13 +01:00
services.samba = {
enable = true;
openFirewall = true;
2024-10-23 14:41:27 +02:00
winbindd.enable = false;
nmbd.enable = false;
2024-10-10 19:11:21 +02:00
settings =
2024-07-26 22:12:48 +02:00
let
mkShare =
{
name,
user ? "smb",
group ? "smb",
hasBunker ? false,
hasPaperless ? false,
persistRoot ? "/panzer",
}:
cfg:
let
config = {
"#persistRoot" = persistRoot;
"#user" = user;
"#group" = group;
"read only" = "no";
"guest ok" = "no";
"create mask" = "0740";
"directory mask" = "0750";
"force user" = user;
"force group" = group;
"valid users" = "${user} @${group}";
"force create mode" = "0660";
"force directory mode" = "0770";
# Might be necessary for windows user to be able to open thing in smb
"acl allow execute always" = "no";
} // cfg;
in
2024-01-02 15:57:33 +01:00
{
2024-07-26 22:12:48 +02:00
"${name}" = config // {
"path" = "/media/smb/${name}";
};
2024-01-02 15:57:33 +01:00
}
2024-07-26 22:12:48 +02:00
// lib.optionalAttrs hasBunker {
"${name}-important" = config // {
2024-01-15 20:46:53 +01:00
"path" = "/media/smb/${name}-important";
"#persistRoot" = "/bunker";
};
2024-07-26 22:12:48 +02:00
}
// lib.optionalAttrs hasPaperless {
"${name}-paperless" = config // {
2024-01-18 00:39:25 +01:00
"path" = "/media/smb/${name}-paperless";
"#paperless" = true;
"force user" = "paperless";
"force group" = "paperless";
# Empty to prevent imperamence setting a persistence folder
"#persistRoot" = "";
};
2024-07-26 22:12:48 +02:00
};
in
2024-01-02 15:57:33 +01:00
lib.mkMerge [
2024-10-10 19:11:21 +02:00
{
global = {
logging = "systemd";
2024-10-23 14:41:27 +02:00
security = "user";
2024-10-10 19:11:21 +02:00
"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.
"netbios name" = "patricks-tolles-nas";
# Disable netbios support. We don't need to support browsing since all
# clients hardcode the host and share names.
"disable netbios" = "yes";
# Allow access to local network
# Also allow printer access
"hosts allow" = "10.99.10. ${lib.net.cidr.host 32 globals.net.vlans.devices.cidrv4} localhost";
2024-10-10 19:11:21 +02:00
"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";
# Do not map the executable bit to anything
"map archive" = "no";
"map system" = "no";
"map hidden" = "no";
# 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";
};
}
2024-01-02 15:57:33 +01:00
(mkShare {
name = "ggr-data";
user = "ggr";
group = "ggr";
2024-01-15 02:13:46 +01:00
hasBunker = true;
2024-07-26 22:12:48 +02:00
} { })
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-07-26 22:12:48 +02: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-07-26 22:12:48 +02:00
} { })
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-07-26 22:12:48 +02:00
} { })
2024-12-23 17:03:10 +01:00
(mkShare
{
name = "printer";
user = "printer";
group = "printer";
}
{
}
)
2024-01-11 13:09:16 +01:00
(mkShare {
name = "family-data";
user = "family";
group = "family";
2024-07-26 22:12:48 +02:00
} { })
(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
{
"guest ok" = "yes";
2024-01-02 15:57:33 +01:00
"read only" = "yes";
2024-01-11 13:09:16 +01:00
"write list" = "@family";
2024-07-26 22:12:48 +02:00
}
)
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 = {
2024-04-11 23:11:53 +02:00
rekeyFile = config.node.secretsDir + "/smbpassdb.tdb.age";
2023-11-03 22:59:13 +01:00
};
2024-07-26 22:12:48 +02:00
users =
let
2024-10-10 19:11:21 +02:00
users = lib.unique (lib.flatten (lib.mapAttrsToList (_: val: val."force user") shares));
2024-07-26 22:12:48 +02:00
groups = lib.unique (
2024-10-10 19:11:21 +02:00
users ++ (lib.flatten (lib.mapAttrsToList (_: val: val."force group") shares))
2024-07-26 22:12:48 +02:00
);
in
{
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; } ]
);
groups = lib.mkMerge (
(lib.flip map groups (group: {
${group} = { };
}))
++ [
{
family.members = [
"patrick"
"david"
"helen"
"ggr"
];
printer.members = [
"patrick"
"david"
"helen"
"ggr"
];
}
]
);
};
fileSystems = lib.mkMerge (
2024-10-10 19:11:21 +02:00
lib.flip lib.mapAttrsToList shares (
2024-07-26 22:12:48 +02:00
_: v:
2024-10-23 14:41:27 +02:00
lib.optionalAttrs ((v ? "#paperless") && v."#paperless") {
"${v.path}/consume" = {
2024-07-26 22:12:48 +02:00
fsType = "none";
options = [ "bind" ];
2024-10-23 14:41:27 +02:00
device = "/paperless/consume/${v."#user"}";
2024-01-18 00:39:25 +01:00
};
2024-10-23 14:41:27 +02:00
"${v.path}/media/archive" = {
2024-07-26 22:12:48 +02:00
fsType = "none ";
options = [
"bind"
"ro"
];
2024-10-23 14:41:27 +02:00
device = "/paperless/media/documents/archive/${v."#user"}";
2024-01-15 02:13:46 +01:00
};
2024-10-23 14:41:27 +02:00
"${v.path}/media/originals" = {
2024-07-26 22:12:48 +02:00
fsType = "none ";
options = [
"bind"
"ro"
];
2024-10-23 14:41:27 +02:00
device = "/paperless/media/documents/originals/${v."#user"}";
2024-07-26 22:12:48 +02:00
};
}
)
);
2024-01-18 00:39:25 +01:00
2024-07-26 22:12:48 +02:00
systemd.tmpfiles.settings = lib.mkMerge (
2024-10-10 19:11:21 +02:00
lib.flip lib.mapAttrsToList shares (
2024-07-26 22:12:48 +02:00
_: v:
2024-10-23 14:41:27 +02:00
lib.optionalAttrs ((v ? "#paperless") && v."#paperless") {
2024-07-26 22:12:48 +02:00
"10-smb-paperless"."/paperless/consume/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-10-23 14:41:27 +02:00
"10-smb-paperless"."/paperless/consume/${v."#user"}".d = {
2024-07-26 22:12:48 +02:00
user = "paperless";
group = "paperless";
mode = "0770";
};
"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";
};
2024-01-19 22:33:03 +01:00
2024-07-26 22:12:48 +02:00
"10-smb-paperless"."/paperless/media/documents/archive/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-10-23 14:41:27 +02:00
"10-smb-paperless"."/paperless/media/documents/archive/${v."#user"}".d = {
2024-07-26 22:12:48 +02:00
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-10-23 14:41:27 +02:00
"10-smb-paperless"."/paperless/media/documents/archive/${v."#user"}/.keep".f = {
2024-07-26 22:12:48 +02:00
user = "paperless";
group = "paperless";
mode = "0660";
};
"10-smb-paperless"."/paperless/media/documents/originals/".d = {
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-10-23 14:41:27 +02:00
"10-smb-paperless"."/paperless/media/documents/originals/${v."#user"}".d = {
2024-07-26 22:12:48 +02:00
user = "paperless";
group = "paperless";
mode = "0770";
};
2024-10-23 14:41:27 +02:00
"10-smb-paperless"."/paperless/media/documents/originals/${v."#user"}/.keep".f = {
2024-07-26 22:12:48 +02:00
user = "paperless";
group = "paperless";
mode = "0660";
};
}
2024-07-26 22:12:48 +02:00
)
);
environment.persistence = lib.mkMerge (
lib.flatten [
2024-10-10 19:11:21 +02:00
(lib.flip lib.mapAttrsToList shares (
2024-07-26 22:12:48 +02:00
_: v:
2024-10-23 14:41:27 +02:00
lib.optionalAttrs ((v ? "#persistRoot") && (v."#persistRoot" != "")) {
${v."#persistRoot"}.directories = [
2024-07-26 22:12:48 +02:00
{
2024-10-23 14:41:27 +02:00
directory = "${v.path}";
user = "${v."force user"}";
group = "${v."force group"}";
2024-07-26 22:12:48 +02:00
mode = "0770";
}
];
}
))
]
);
2023-11-03 22:59:13 +01:00
}