summary refs log tree commit diff
path: root/modules/tasks/tty-backgrounds.nix
blob: 265c7a57bf2d67499769f4edfb84d9958cadcc46 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{ config, pkgs, ... }:

with pkgs.lib;

let

  inherit (pkgs) stdenv;
  
  kernelPackages = config.boot.kernelPackages;
  splashutils = kernelPackages.splashutils;
  requiredTTYs = config.requiredTTYs;
  
  backgrounds =
    let
    
      specificThemes = config.services.ttyBackgrounds.specificThemes;
        
      overridenTTYs = map (x: x.tty) specificThemes;

      # Use the default theme for all the mingetty ttys and for the
      # syslog tty, except those for which a specific theme is
      # specified.
      defaultTTYs =
        filter (x: !(elem x overridenTTYs)) requiredTTYs;

    in      
      (map (tty: { inherit tty; }) defaultTTYs) ++ specificThemes;

  themesUnpacked = stdenv.mkDerivation {
    name = "splash-themes";
    builder = ./tty-backgrounds-combine.sh;
    default = unpackTheme config.services.ttyBackgrounds.defaultTheme;
    # !!! Should use XML here.
    ttys = map (x: x.tty) backgrounds;
    themes = map (x: if x ? theme then unpackTheme x.theme else "default") backgrounds;
  };

  unpackTheme = theme: import ../../lib/unpack-theme.nix {
    inherit stdenv theme;
  };

in

{

  ###### interface
  
  options = {
  
    services.ttyBackgrounds = {

      enable = mkOption {
        default = splashutils != null;
        description = ''
          Whether to enable graphical backgrounds for the virtual consoles.
        '';
      };

      defaultTheme = mkOption {
        default = pkgs.fetchurl {
          #url = http://www.bootsplash.de/files/themes/Theme-BabyTux.tar.bz2;
          url = mirror://gentoo/distfiles/Theme-BabyTux.tar.bz2;
          md5 = "a6d89d1c1cff3b6a08e2f526f2eab4e0";
        };
        description = ''
          The default theme for the virtual consoles.  Themes can be found
          at <link xlink:href='http://www.bootsplash.de/' />.
        '';
      };

      specificThemes = mkOption {
        default = [ ];
        description = ''
          This option overrides the theme for specific virtual consoles.
        '';
      };
      
    };
    
  };


  ###### implementation

  config = mkIf config.services.ttyBackgrounds.enable (
  mkAssert (splashutils != null) "
    The kernelPackages does not provide splashutils, as required by ttyBackgrounds.
    Either provide kernelPackages with splashutils, or disable ttyBackgrounds.
  " {

    services.ttyBackgrounds.specificThemes = singleton
      { tty = config.services.syslogd.tty;
        theme = pkgs.themes "theme-gnu";
      };

    environment.etc = singleton
      { source = themesUnpacked;
        target = "splash";
      };

    jobs.ttyBackgrounds =
      { name = "tty-backgrounds";

        startOn = "started udev";

        # Don't stop tty-backgrounds during shutdown; it's not needed,
        # and it just causes intrusive blanking of the screen.
        stopOn = "";

        preStart =
          ''
            # Critical: tell the kernel where to find splash_helper.  It calls
            # this program every time we switch between consoles.
            helperProcFile=${splashutils.helperProcFile}
            if test -e /proc/sys/fbcondecor; then helperProcFile=/proc/sys/fbcondecor; fi
            echo ${splashutils}/${splashutils.helperName} > $helperProcFile

            # For each console...
            for tty in ${toString (map (x: x.tty) backgrounds)}; do
                # Make sure that the console exists.
                echo -n "" > /dev/$tty

                # Set the theme as determined by tty-backgrounds-combine.sh
                # above.  Note that splashutils needs a TTY number
                # instead of a device name, hence the ''${tty:3}.
                theme=$(readlink ${themesUnpacked}/$tty)
                prevTheme=$(${splashutils}/${splashutils.controlName} --tty ''${tty:3} -c getcfg |
                    sed 's/theme: *\(.*\)/\1/;t;d' || true)
                if [ "$theme" != "$prevTheme" ]; then
                    ${splashutils}/${splashutils.controlName} --tty ''${tty:3} -c setcfg -t $theme || true
                fi
                ${splashutils}/${splashutils.controlName} --tty ''${tty:3} -c setpic -t $theme || true
                ${splashutils}/${splashutils.controlName} --tty ''${tty:3} -c on || true
            done
          '';

        postStop =
          ''
            # Disable the theme on each console.
            for tty in ${toString (map (x: x.tty) backgrounds)}; do
                ${splashutils}/${splashutils.controlName} --tty ''${tty:3} -c off || true
            done
          '';
      };
    
  });
  
}