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. 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. "local master" = "no";
  27. "domain master" = "no";
  28. "preferred master" = "no";
  29. };
  30. } // builtins.listToAttrs
  31. (map
  32. (name: {
  33. inherit name;
  34. value = {
  35. path = "/mnt/storage/samba/${name}";
  36. "preserve case" = "yes";
  37. "short preserve case" = "yes";
  38. "browseable" = "yes";
  39. "writeable" = "yes";
  40. "read only" = "no";
  41. "guest ok" = "no";
  42. "create mask" = "0644";
  43. "directory mask" = "0755";
  44. "valid users" = "${name}";
  45. "fruit:aapl" = "yes";
  46. "vfs objects" = "catia fruit streams_xattr";
  47. };
  48. })
  49. shares);
  50. };
  51. systemd.tmpfiles.rules = [
  52. "d /mnt/storage/samba 0755 root storage -"
  53. ] ++ map (name: "d /mnt/storage/samba/${name} 0770 ${name} storage -") shares;
  54. system.activationScripts.addSambaUsers = {
  55. text = ''
  56. #!/bin/sh
  57. USERS="${config.age.secrets."odin/services/samba".path}"
  58. if [ -f "$USERS" ]; then
  59. while IFS=, read -r username password; do
  60. if [ -z "$username" ] || [ -z "$password" ]; then
  61. continue
  62. fi
  63. # Check if the user exists in the system
  64. if id "$username" >/dev/null 2>&1; then
  65. # Add or update the Samba user password
  66. echo -e "$password\n$password" | ${lib.getExe' pkgs.samba "smbpasswd"} -s -a "$username"
  67. echo "Added/Updated Samba user: $username"
  68. else
  69. echo "System user $username does not exist, skipping..."
  70. fi
  71. done < "$USERS"
  72. else
  73. echo "Samba users CSV file not found at $USERS"
  74. fi
  75. '';
  76. deps = [ "users" ];
  77. };
  78. }