about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/tasks/kbd.nix
blob: c6ba998b19e63e30209944c4d2e7e55c435faf46 (plain) (blame)
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
{ config, lib, pkgs, ... }:

with lib;

let

  makeColor = n: value: "COLOR_${toString n}=${value}";
  makeColorCS =
    let positions = [ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ];
    in n: value: "\\033]P${elemAt positions (n - 1)}${value}";
  colors = concatImapStringsSep "\n" makeColor config.i18n.consoleColors;

  isUnicode = hasSuffix "UTF-8" (toUpper config.i18n.defaultLocale);

  optimizedKeymap = pkgs.runCommand "keymap" {
    nativeBuildInputs = [ pkgs.buildPackages.kbd ];
    LOADKEYS_KEYMAP_PATH = "${kbdEnv}/share/keymaps/**";
    preferLocalBuild = true;
  } ''
    loadkeys -b ${optionalString isUnicode "-u"} "${config.i18n.consoleKeyMap}" > $out
  '';

  # Sadly, systemd-vconsole-setup doesn't support binary keymaps.
  vconsoleConf = pkgs.writeText "vconsole.conf" ''
    KEYMAP=${config.i18n.consoleKeyMap}
    FONT=${config.i18n.consoleFont}
    ${colors}
  '';

  kbdEnv = pkgs.buildEnv {
    name = "kbd-env";
    paths = [ pkgs.kbd ] ++ config.i18n.consolePackages;
    pathsToLink = [ "/share/consolefonts" "/share/consoletrans" "/share/keymaps" "/share/unimaps" ];
  };

  setVconsole = !config.boot.isContainer;
in

{
  ###### interface

  options = {

    # most options are defined in i18n.nix

    # FIXME: still needed?
    boot.extraTTYs = mkOption {
      default = [];
      type = types.listOf types.str;
      example = ["tty8" "tty9"];
      description = ''
        Tty (virtual console) devices, in addition to the consoles on
        which mingetty and syslogd run, that must be initialised.
        Only useful if you have some program that you want to run on
        some fixed console.  For example, the NixOS installation CD
        opens the manual in a web browser on console 7, so it sets
        <option>boot.extraTTYs</option> to <literal>["tty7"]</literal>.
      '';
    };

    boot.earlyVconsoleSetup = mkOption {
      default = false;
      type = types.bool;
      description = ''
        Enable setting font as early as possible (in initrd).
      '';
    };

  };


  ###### implementation

  config = mkMerge [
    (mkIf (!setVconsole) {
      systemd.services.systemd-vconsole-setup.enable = false;
    })

    (mkIf setVconsole (mkMerge [
      { environment.systemPackages = [ pkgs.kbd ];

        # Let systemd-vconsole-setup.service do the work of setting up the
        # virtual consoles.
        environment.etc."vconsole.conf".source = vconsoleConf;
        # Provide kbd with additional packages.
        environment.etc.kbd.source = "${kbdEnv}/share";

        boot.initrd.preLVMCommands = mkBefore ''
          kbd_mode ${if isUnicode then "-u" else "-a"} -C /dev/console
          printf "\033%%${if isUnicode then "G" else "@"}" >> /dev/console
          loadkmap < ${optimizedKeymap}

          ${optionalString config.boot.earlyVconsoleSetup ''
            setfont -C /dev/console $extraUtils/share/consolefonts/font.psf
          ''}

          ${concatImapStringsSep "\n" (n: color: ''
            printf "${makeColorCS n color}" >> /dev/console
          '') config.i18n.consoleColors}
        '';

        systemd.services.systemd-vconsole-setup =
          { before = [ "display-manager.service" ];
            after = [ "systemd-udev-settle.service" ];
            restartTriggers = [ vconsoleConf kbdEnv ];
          };
      }

      (mkIf config.boot.earlyVconsoleSetup {
        boot.initrd.extraUtilsCommands = ''
          mkdir -p $out/share/consolefonts
          ${if substring 0 1 config.i18n.consoleFont == "/" then ''
            font="${config.i18n.consoleFont}"
          '' else ''
            font="$(echo ${kbdEnv}/share/consolefonts/${config.i18n.consoleFont}.*)"
          ''}
          if [[ $font == *.gz ]]; then
            gzip -cd $font > $out/share/consolefonts/font.psf
          else
            cp -L $font $out/share/consolefonts/font.psf
          fi
        '';
      })
    ]))
  ];

}