Samba: Difference between revisions
IFreilicht (talk | contribs) Updated section on Time Machine |
→User Authentication: add robust automated authentication example that provides full context and aims to give a basic, typical password-protected folder share |
||
| (9 intermediate revisions by 7 users not shown) | |||
| Line 8: | Line 8: | ||
services.samba = { | services.samba = { | ||
enable = true; | enable = true; | ||
openFirewall = true; | openFirewall = true; | ||
settings = { | |||
workgroup = WORKGROUP | global = { | ||
"workgroup" = "WORKGROUP"; | |||
"server string" = "smbnix"; | |||
"netbios name" = "smbnix"; | |||
"security" = "user"; | |||
#"use sendfile" = "yes"; | |||
#"max protocol" = "smb2"; | |||
# note: localhost is the ipv6 localhost ::1 | |||
"hosts allow" = "192.168.0. 127.0.0.1 localhost"; | |||
"hosts deny" = "0.0.0.0/0"; | |||
"guest account" = "nobody"; | |||
"map to guest" = "bad user"; | |||
}; | |||
public = { | "public" = { | ||
path = "/mnt/Shares/Public"; | "path" = "/mnt/Shares/Public"; | ||
browseable = "yes"; | "browseable" = "yes"; | ||
"read only" = "no"; | "read only" = "no"; | ||
"guest ok" = "yes"; | "guest ok" = "yes"; | ||
| Line 34: | Line 33: | ||
"force group" = "groupname"; | "force group" = "groupname"; | ||
}; | }; | ||
private = { | "private" = { | ||
path = "/mnt/Shares/Private"; | "path" = "/mnt/Shares/Private"; | ||
browseable = "yes"; | "browseable" = "yes"; | ||
"read only" = "no"; | "read only" = "no"; | ||
"guest ok" = "no"; | "guest ok" = "no"; | ||
| Line 52: | Line 51: | ||
}; | }; | ||
services.avahi = { | |||
publish.enable = true; | |||
publish.userServices = true; | |||
# ^^ Needed to allow samba to automatically register mDNS records (without the need for an `extraServiceFile` | |||
nssmdns4 = true; | |||
# ^^ Not one hundred percent sure if this is needed- if it aint broke, don't fix it | |||
enable = true; | |||
openFirewall = true; | |||
}; | |||
networking.firewall.enable = true; | networking.firewall.enable = true; | ||
| Line 71: | Line 69: | ||
=== User Authentication === | === User Authentication === | ||
For a user called <code>my_user</code>to be authenticated on the samba server, you | For a user called <code>my_user</code>to be authenticated on the samba server, you can add a password using: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
smbpasswd -a my_user | sudo smbpasswd -a my_user | ||
</syntaxhighlight> | </syntaxhighlight> | ||
To automate creation of the samba user and the required system user, you can use [https://search.nixos.org/options?show=system.activationScripts system.activationScripts]: | |||
<syntaxhighlight lang="nix"> | |||
{ | |||
# Make the samba user "my_user" on the system | |||
users.users.my_user = { | |||
description = "Write-access to samba media shares"; | |||
# Add this user to a group with permission to access the expected files | |||
extraGroups = [ "users" ]; | |||
# Password can be set in clear text with a literal string or from a file. | |||
# Using sops-nix we can use the same file so that the system user and samba | |||
# user share the same credential (if desired). | |||
hashedPasswordFile = config.sops.secrets.samba.path; | |||
isNormalUser = true; | |||
}; | |||
# Set "my_user" as a valid samba login | |||
services.samba = { | |||
enable = true; | |||
securityType = "user"; | |||
openFirewall = true; | |||
settings.my_share_directory = { | |||
# ... | |||
"valid users" = "my_user"; | |||
}; | |||
}; | |||
# Activation scripts run every time nixos switches build profiles. So if you're | |||
# pulling the user/samba password from a file then it will be updated during | |||
# nixos-rebuild. Again, in this example we're using sops-nix with a "samba" entry | |||
# to avoid cleartext password, but this could be replaced with a static path. | |||
system.activationScripts = { | |||
# The "init_smbpasswd" script name is arbitrary, but a useful label for tracking | |||
# failed scripts in the build output. An absolute path to smbpasswd is necessary | |||
# as it is not in $PATH in the activation script's environment. The password | |||
# is repeated twice with newline characters as smbpasswd requires a password | |||
# confirmation even in non-interactive mode where input is piped in through stdin. | |||
init_smbpasswd.text = '' | |||
/run/current-system/sw/bin/printf "$(/run/current-system/sw/bin/cat ${config.sops.secrets.samba.path})\n$(/run/current-system/sw/bin/cat ${config.sops.secrets.samba.path})\n" | /run/current-system/sw/bin/smbpasswd -sa my_user | |||
''; | |||
}; | |||
} | |||
</syntaxhighlight> | |||
=== Configuration === | === Configuration === | ||
| Line 85: | Line 128: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
services.samba = { | services.samba = { | ||
settings = { | |||
tm_share = { | "tm_share" = { | ||
path = "/mnt/Shares/tm_share"; | "path" = "/mnt/Shares/tm_share"; | ||
"valid users" = "username"; | "valid users" = "username"; | ||
public = "no"; | "public" = "no"; | ||
writeable = "yes"; | "writeable" = "yes"; | ||
"force user" = "username"; | "force user" = "username"; | ||
# | # Below are the most imporant for macOS compatibility | ||
# Change the above to suit your needs | |||
"fruit:aapl" = "yes"; | "fruit:aapl" = "yes"; | ||
"fruit:time machine" = "yes"; | "fruit:time machine" = "yes"; | ||
| Line 107: | Line 151: | ||
<!DOCTYPE service-group SYSTEM "avahi-service.dtd"> | <!DOCTYPE service-group SYSTEM "avahi-service.dtd"> | ||
<service-group> | <service-group> | ||
<name>%h</name> | <name replace-wildcards="yes">%h</name> | ||
<service> | <service> | ||
<type>_smb._tcp</type> | <type>_smb._tcp</type> | ||
| Line 115: | Line 159: | ||
<type>_device-info._tcp</type> | <type>_device-info._tcp</type> | ||
<port>0</port> | <port>0</port> | ||
<txt-record> | <txt-record>model=TimeCapsule8,119</txt-record> | ||
</service> | </service> | ||
<service> | <service> | ||
<type>_adisk._tcp</type> | <type>_adisk._tcp</type> | ||
<txt-record>dk0=adVN=tm_share</txt-record> | <!-- | ||
<txt-record>sys=waMa=0</txt-record> | change tm_share to share name, if you changed it. | ||
--> | |||
<txt-record>dk0=adVN=tm_share,adVF=0x82</txt-record> | |||
<txt-record>sys=waMa=0,adVF=0x100</txt-record> | |||
</service> | </service> | ||
</service-group> | </service-group> | ||
| Line 141: | Line 188: | ||
package = pkgs.sambaFull; | package = pkgs.sambaFull; | ||
openFirewall = true; | openFirewall = true; | ||
settings = { | |||
load printers = yes | "global" = { | ||
"load printers" = "yes"; | |||
"printing" = "cups"; | |||
"printcap name" = "cups"; | |||
}; | |||
printers = { | "printers" = { | ||
comment = "All Printers"; | "comment" = "All Printers"; | ||
path = "/var/spool/samba"; | "path" = "/var/spool/samba"; | ||
public = "yes"; | "public" = "yes"; | ||
browseable = "yes"; | "browseable" = "yes"; | ||
# to allow user 'guest account' to print. | # to allow user 'guest account' to print. | ||
"guest ok" = "yes"; | "guest ok" = "yes"; | ||
writable = "no"; | "writable" = "no"; | ||
printable = "yes"; | "printable" = "yes"; | ||
"create mode" = 0700; | "create mode" = 0700; | ||
}; | }; | ||
| Line 205: | Line 252: | ||
# Set pythonpath manually (bellow with overrideAttrs) as it is not set on 22.11 due to bug | # Set pythonpath manually (bellow with overrideAttrs) as it is not set on 22.11 due to bug | ||
}).overrideAttrs (finalAttrs: previousAttrs: { | }).overrideAttrs (finalAttrs: previousAttrs: { | ||
pythonPath = with super; [ python3Packages.dnspython tdb ldb talloc ]; | pythonPath = with super; [ python3Packages.dnspython python3Packages.markdown tdb ldb talloc ]; | ||
}); | }); | ||
})]; | })]; | ||
| Line 377: | Line 424: | ||
services.gvfs = { | services.gvfs = { | ||
enable = true; | enable = true; | ||
package = lib.mkForce pkgs. | package = lib.mkForce pkgs.gnome.gvfs; | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||