summary refs log tree commit diff
path: root/modules/tasks/kbd.nix
blob: 42511a76156e7017761e04471dc7873daf191a20 (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
128
129
{pkgs, config, ...}:

let

###### interface

  # most options are defined in i18n.nix

  options = {

    boot.extraTTYs = pkgs.lib.mkOption {
      default = [];
      example = [8 9];
      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>[7]</literal>.
      ";
    };
    
    # dummy option so that requiredTTYs can be passed
    requiredTTYs = pkgs.lib.mkOption {
      default = [];
      description = "
        FIXME: find another place for this option.
        FIXME: find a good description.
      ";
    };

  };

  
###### implementation

  # think about where to put this chunk of code!
  # required by other pieces as well
  requiredTTYs = config.services.mingetty.ttys
    ++ config.boot.extraTTYs
    ++ [config.services.syslogd.tty];
  ttyNumbers = requiredTTYs;
  ttys = map (nr: "/dev/tty" + toString nr) ttyNumbers;
  defaultLocale = config.i18n.defaultLocale;
  consoleFont = config.i18n.consoleFont;
  consoleKeyMap = config.i18n.consoleKeyMap;

in

{
  require = [options];

  inherit requiredTTYs; # pass them to ./modules/tasks/tty-backgrounds.nix

  services = {
    extraJobs = [{
      name = "kbd";

      extraPath = [
        pkgs.kbd
      ];
      
      job = "
        description \"Keyboard / console initialisation\"

        start on udev

        script

          export LANG=${defaultLocale}
          export PATH=${pkgs.gzip}/bin:$PATH # Needed by setfont

          set +e # continue in case of errors

          
          # Enable or disable UTF-8 mode.  This is based on
          # unicode_{start,stop}.
          echo 'Enabling or disabling Unicode mode...'

          charMap=$(${pkgs.glibc}/bin/locale charmap)

          if test \"$charMap\" = UTF-8; then

            for tty in ${toString ttys}; do

              # Tell the console output driver that the bytes arriving are
              # UTF-8 encoded multibyte sequences. 
              echo -n -e '\\033%G' > $tty

            done

            # Set the keyboard driver in UTF-8 mode.
            ${pkgs.kbd}/bin/kbd_mode -u

          else

            for tty in ${toString ttys}; do

              # Tell the console output driver that the bytes arriving are
              # UTF-8 encoded multibyte sequences. 
              echo -n -e '\\033%@' > $tty

            done

            # Set the keyboard driver in ASCII (or any 8-bit character
            # set) mode.
            ${pkgs.kbd}/bin/kbd_mode -a

          fi


          # Set the console font.
          for tty in ${toString ttys}; do
            ${pkgs.kbd}/bin/setfont -C $tty ${consoleFont}
          done


          # Set the keymap.
          ${pkgs.kbd}/bin/loadkeys '${consoleKeyMap}'


        end script
      ";
    
    }];
  };

}