summaryrefslogtreecommitdiff
path: root/openwrt/users.nix
blob: 6e4f6fcaaccdeb1f6548a8b6aac909a78a6f1582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{ config, lib, ... }:

{
  options.users.root.hashedPassword = lib.mkOption {
    type = lib.types.nullOr (lib.types.strMatching "[^\n:]*");
    default = null;
    description = ''
      Hashed password of the user. This should be either a disabled password
      (e.g. `*` or `!`) or use MD5, SHA256, or SHA512.
    '';
  };

  config = {
    deploySteps.rootPassword = lib.mkIf (config.users.root.hashedPassword != null) {
      priority = 5000;
      apply = ''
        (
          umask 0077
          touch /tmp/.shadow
          while IFS=: read name pw rest; do
            if [ "$name" = root ]; then
              echo "$name:"${lib.escapeShellArg config.users.root.hashedPassword}":$rest"
            else
              echo "$name:$pw:$rest"
            fi
          done </etc/shadow >>/tmp/.shadow
          mv /tmp/.shadow /etc/shadow
        )
      '';
    };
  };
}