1 { config, lib, pkgs, ... }:
7 cfg = config.security.sudo;
9 toUserString = user: if (isInt user) then "#${toString user}" else "${user}";
10 toGroupString = group: if (isInt group) then "%#${toString group}" else "%${group}";
12 toCommandOptionsString = options:
13 "${concatStringsSep ":" options}${optionalString (length options != 0) ":"} ";
15 toCommandsString = commands:
16 concatStringsSep ", " (
18 if (isString command) then
21 "${toCommandOptionsString command.options}${command.command}"
31 options.security.sudo = {
33 defaultOptions = mkOption {
34 type = with types; listOf str;
35 default = [ "SETENV" ];
37 Options used for the default rules, granting `root` and the
38 `wheel` group permission to run any command as any user.
46 Whether to enable the {command}`sudo` command, which
47 allows non-root users to execute commands as root.
51 package = mkPackageOption pkgs "sudo" { };
53 wheelNeedsPassword = mkOption {
57 Whether users of the `wheel` group must
58 provide a password to run commands as super user via {command}`sudo`.
62 execWheelOnly = mkOption {
66 Only allow members of the `wheel` group to execute sudo by
67 setting the executable's permissions accordingly.
68 This prevents users that are not members of `wheel` from
69 exploiting vulnerabilities in sudo such as CVE-2021-3156.
73 configFile = mkOption {
75 # Note: if syntax errors are detected in this file, the NixOS
76 # configuration will fail to build.
78 This string contains the contents of the
83 extraRules = mkOption {
85 Define specific rules to be in the {file}`sudoers` file.
86 More specific rules should come after more general ones in order to
87 yield the expected behavior. You can use mkBefore/mkAfter to ensure
88 this is the case when configuration options are merged.
91 example = literalExpression ''
93 # Allow execution of any command by all users in group sudo,
94 # requiring a password.
95 { groups = [ "sudo" ]; commands = [ "ALL" ]; }
97 # Allow execution of "/home/root/secret.sh" by user `backup`, `database`
98 # and the group with GID `1006` without a password.
99 { users = [ "backup" "database" ]; groups = [ 1006 ];
100 commands = [ { command = "/home/root/secret.sh"; options = [ "SETENV" "NOPASSWD" ]; } ]; }
102 # Allow all users of group `bar` to run two executables as user `foo`
103 # with arguments being pre-set.
104 { groups = [ "bar" ]; runAs = "foo";
106 [ "/home/baz/cmd1.sh hello-sudo"
107 { command = '''/home/baz/cmd2.sh ""'''; options = [ "SETENV" ]; } ]; }
110 type = with types; listOf (submodule {
113 type = with types; listOf (either str int);
115 The usernames / UIDs this rule should apply for.
121 type = with types; listOf (either str int);
123 The groups / GIDs this rule should apply for.
132 For what host this rule should apply.
137 type = with types; str;
140 Under which user/group the specified command is allowed to run.
142 A user can be specified using just the username: `"foo"`.
143 It is also possible to specify a user/group combination using `"foo:bar"`
144 or to only allow running as a specific group with `":bar"`.
148 commands = mkOption {
150 The commands for which the rule should apply.
152 type = with types; listOf (either str (submodule {
156 type = with types; str;
158 A command being either just a path to a binary to allow any arguments,
159 the full command with arguments pre-set or with `""` used as the argument,
160 not allowing arguments to the command at all.
165 type = with types; listOf (enum [ "NOPASSWD" "PASSWD" "NOEXEC" "EXEC" "SETENV" "NOSETENV" "LOG_INPUT" "NOLOG_INPUT" "LOG_OUTPUT" "NOLOG_OUTPUT" "MAIL" "NOMAIL" "FOLLOW" "NOFLLOW" "INTERCEPT" "NOINTERCEPT"]);
167 Options for running the command. Refer to the [sudo manual](https://www.sudo.ws/docs/man/1.9.15/sudoers.man/#Tag_Spec).
179 extraConfig = mkOption {
183 Extra configuration text appended to {file}`sudoers`.
189 ###### implementation
191 config = mkIf cfg.enable {
193 assertion = cfg.package.pname != "sudo-rs";
195 NixOS' `sudo` module does not support `sudo-rs`; see `security.sudo-rs` instead.
199 security.sudo.extraRules =
201 defaultRule = { users ? [], groups ? [], opts ? [] }: [ {
202 inherit users groups;
205 options = opts ++ cfg.defaultOptions;
209 # This is ordered before users' `mkBefore` rules,
210 # so as not to introduce unexpected changes.
211 (mkOrder 400 (defaultRule { users = [ "root" ]; }))
213 # This is ordered to show before (most) other rules, but
214 # late-enough for a user to `mkBefore` it.
215 (mkOrder 600 (defaultRule {
216 groups = [ "wheel" ];
217 opts = (optional (!cfg.wheelNeedsPassword) "NOPASSWD");
221 security.sudo.configFile = concatStringsSep "\n" (filter (s: s != "") [
223 # Don't edit this file. Set the NixOS options ‘security.sudo.configFile’
224 # or ‘security.sudo.extraRules’ instead.
226 (pipe cfg.extraRules [
227 (filter (rule: length rule.commands != 0))
229 (map (user: "${toUserString user} ${rule.host}=(${rule.runAs}) ${toCommandsString rule.commands}") rule.users)
230 (map (group: "${toGroupString group} ${rule.host}=(${rule.runAs}) ${toCommandsString rule.commands}") rule.groups)
233 (concatStringsSep "\n")
236 (optionalString (cfg.extraConfig != "") ''
242 security.wrappers = let
244 group = if cfg.execWheelOnly then "wheel" else "root";
246 permissions = if cfg.execWheelOnly then "u+rx,g+x" else "u+rx,g+x,o+x";
249 source = "${cfg.package.out}/bin/sudo";
250 inherit owner group setuid permissions;
253 source = "${cfg.package.out}/bin/sudoedit";
254 inherit owner group setuid permissions;
258 environment.systemPackages = [ cfg.package ];
260 security.pam.services.sudo = { sshAgentAuth = true; usshAuth = true; };
262 environment.etc.sudoers =
264 pkgs.runCommand "sudoers"
266 src = pkgs.writeText "sudoers-in" cfg.configFile;
267 preferLocalBuild = true;
269 # Make sure that the sudoers file is syntactically valid.
270 # (currently disabled - NIXOS-66)
271 "${pkgs.buildPackages.sudo}/sbin/visudo -f $src -c && cp $src $out";