samba.nix 2.3 KB

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