1
0

samba.nix 2.3 KB

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