~ruther/nixos-config

ref: a1128e507b85a32d4a700727c24d11151284a954 nixos-config/home/modules/sessions/default.nix -rw-r--r-- 5.0 KiB
a1128e50 — Frantisek Bohacek feat(home): add desktop session startup options 8 months ago
                                                                                
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{ config, lib, pkgs, ... }:

let
  cfg = config.desktopSessions;

  availableSessionsConfig = lib.filterAttrs (name: x: x.enable) cfg.instances;
  availableSessions = lib.attrNames availableSessionsConfig;
  sessionStartScripts = lib.map (x: x.startSession) (lib.attrValues availableSessionsConfig);

  desktopSessionType = lib.types.submodule ({ config, name, ... }: {
    options = {
      enable = lib.mkEnableOption "desktop session instance enable" // { default = true; };
      name = lib.mkOption {
        type = lib.types.str;
        default = name;
      };

      startSession = lib.mkOption {
        type = lib.types.package;
      };

      preStartHook = lib.mkOption {
        type = lib.types.str;
        default = "";
      };

      postStartHook = lib.mkOption {
        type = lib.types.str;
        default = "";
        description = ''
          This is executed after the session is started.
          However, it's started outside of the session,
          so you won't get any environment variables from
          the session. If you need that, you need to utilize
          configuration, or flags of the session executable.

          There is $pid available to know the pid of the started
          session process.
        '';
      };
      exitHook = lib.mkOption {
        type = lib.types.str;
        default = ''
          systemctl stop --user graphical-session.target
        '';
      };

      executable = lib.mkOption {
        type = lib.types.path;
      };
      arguments = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [];
      };
      environment = lib.mkOption {
        type = lib.types.attrsOf (lib.types.oneOf [ lib.types.str lib.types.number ]);
      };
    };

    config = {
      startSession = pkgs.writeShellScript "start-${name}" ''
        ${lib.concatStringsSep "\n" (lib.attrValues (lib.mapAttrs (name: value: "export ${name}='${value}'") config.environment))}

        ${config.preStartHook}
        ${config.executable} ${lib.concatStringsSep " " config.arguments} &
        pid=$!
        ${config.postStartHook}
        wait $pid
        ${config.exitHook}
      '';
    };
  });
in {
  options.desktopSessions = {
    instances = lib.mkOption {
      type = lib.types.attrsOf desktopSessionType;
      default = {};
    };

    enable = lib.mkEnableOption "desktop session configuration";

    selectSession = lib.mkOption {
      type = lib.types.path;
    };

    defaultSession = lib.mkOption {
      type = lib.types.str;
      default = "tty";
    };

    availableSessions = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      readOnly = true;
    };

    sessionStartScripts = lib.mkOption {
      type = lib.types.listOf lib.types.package;
      readOnly = true;
    };

    sessionStartScriptsPath = lib.mkOption {
      type = lib.types.package;
      readOnly = true;
    };

    autoStart = {
      enable = lib.mkEnableOption "auto start profile selection on tty login" // { default = true; };
      tty = lib.mkOption {
        type = lib.types.int;
        default = 1;
      };
      profileSelectContent = lib.mkOption {
        type = lib.types.str;
      };
    };
  };

  config.desktopSessions = {
    inherit availableSessions;
    inherit sessionStartScripts;

    sessionStartScriptsPath = let
      sessionsCopy = (lib.concatStringsSep "\n" (lib.map (x: ''
            cp "${x.startSession}" "$out/bin/start-${x.name}"
          '') (lib.attrValues availableSessionsConfig)));
    in pkgs.runCommandNoCCLocal "session-start-scripts" {} ''
      mkdir -p $out/bin
      ${sessionsCopy}
    '';

    autoStart.profileSelectContent = ''
      if [[ "$(tty)" == "/dev/tty${builtins.toString cfg.autoStart.tty}" && "$(id -u)" != 0 ]]; then
        ${cfg.selectSession}
      fi
    '';

    selectSession = pkgs.writeShellScript "select-session" ''
      sessions=(${lib.concatStringsSep " " cfg.availableSessions})
      session_indices=(''${!sessions[@]})

      timeout=3
      selected="${cfg.defaultSession}" # default

      echo "Default session to start is $selected"

      echo "Available sessions:"
      for i in ''${!sessions[@]}; do
          echo "  $((i+1))) ''${sessions[$i]}"
      done
      echo "  q) Enter tty."

      echo -n "Choose session to start: "
      read -t"$timeout" -n1 user_input
      echo

      if [[ $user_input == "q" ]]; then
          exit
      elif [[ $user_input ]]; then
          user_input=$((user_input-1))
          echo $user_input
          if [[ " ''${session_indices[@]} " =~ " $user_input " ]]; then
              selected="''${sessions[$user_input]}"
              echo "Got $user_input. Going to start $selected"
          else
              echo "Got unknown option. Exiting."
              exit
          fi
      else
          echo "Got no input, starting $selected"
      fi

      echo "Going to start $selected"

      # tty name is treated specially. It just exits. Use default session of tty,
      # to not start any session.
      if [[ $selected == "tty" ]]; then
       exit
      fi

      ${cfg.sessionStartScriptsPath}/bin/start-$selected
    '';
  };
}
Do not follow this link