samba.nix 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. { config
  2. , lib
  3. , pkgs
  4. , ...
  5. }:
  6. let
  7. shares = [ "thomas" "christine" ];
  8. # TODO: make sure to add system users for all the users that don't exist
  9. in
  10. {
  11. users.users.christine.isSystemUser = true;
  12. users.users.christine.group = "storage";
  13. services.samba = {
  14. enable = true;
  15. openFirewall = true;
  16. settings = {
  17. global = {
  18. workgroup = "WORKGROUP";
  19. "server string" = config.networking.hostName;
  20. "netbios name" = config.networking.hostName;
  21. "security" = "user";
  22. "invalid users" = [ "root" ];
  23. "hosts allow" = "100.64.0.0/10 192.168.178. 127.0.0.1 localhost";
  24. "hosts deny" = "0.0.0.0/0";
  25. "guest account" = "nobody";
  26. "map to guest" = "bad user";
  27. "passdb backend" = "tdbsam";
  28. };
  29. } // builtins.listToAttrs
  30. (map
  31. (name: {
  32. inherit name;
  33. value = {
  34. path = "/mnt/storage/samba/${name}";
  35. "preserve case" = "yes";
  36. "short preserve case" = "yes";
  37. "browseable" = "yes";
  38. "writeable" = "yes";
  39. "read only" = "no";
  40. "guest ok" = "no";
  41. "create mask" = "0644";
  42. "directory mask" = "0755";
  43. "valid users" = "${name}";
  44. "fruit:aapl" = "yes";
  45. "vfs objects" = "catia fruit streams_xattr";
  46. };
  47. })
  48. shares);
  49. };
  50. systemd.tmpfiles.rules = [
  51. "d /mnt/storage/samba 0755 root storage -"
  52. ] ++ map (name: "d /mnt/storage/samba/${name} 0770 ${name} storage -") shares;
  53. system.activationScripts.addSambaUsers = {
  54. text = ''
  55. #!/bin/sh
  56. USERS="${config.age.secrets."odin/services/samba".path}"
  57. if [ -f "$USERS" ]; then
  58. while IFS=, read -r username password; do
  59. if [ -z "$username" ] || [ -z "$password" ]; then
  60. continue
  61. fi
  62. # Check if the user exists in the system
  63. if id "$username" >/dev/null 2>&1; then
  64. # Add or update the Samba user password
  65. echo -e "$password\n$password" | ${lib.getExe' pkgs.samba "smbpasswd"} -s -a "$username"
  66. echo "Added/Updated Samba user: $username"
  67. else
  68. echo "System user $username does not exist, skipping..."
  69. fi
  70. done < "$USERS"
  71. else
  72. echo "Samba users CSV file not found at $USERS"
  73. fi
  74. '';
  75. deps = [ "users" ];
  76. };
  77. }