samba.nix 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. { config
  2. , lib
  3. , pkgs
  4. , ...
  5. }:
  6. let
  7. shares = [ "thomas" "christine" ];
  8. in
  9. {
  10. services.samba = {
  11. enable = true;
  12. openFirewall = true;
  13. settings = {
  14. global = {
  15. "workgroup" = "WORKGROUP";
  16. "server string" = config.networking.hostName;
  17. "netbios name" = config.networking.hostName;
  18. "security" = "user";
  19. "invalid users" = [ "root" ];
  20. "hosts allow" = "100.64.0.0/10 192.168.178. 127.0.0.1 localhost";
  21. "hosts deny" = "0.0.0.0/0";
  22. "guest account" = "nobody";
  23. "map to guest" = "bad user";
  24. "passdb backend" = "tdbsam";
  25. "local master" = "no";
  26. "domain master" = "no";
  27. "preferred master" = "no";
  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" = "0750";
  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 0750 root storage -"
  52. ] ++ map (name: "d /mnt/storage/samba/${name} 0750 ${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. }