zaphyra's git: nixfiles

zaphyra's nixfiles

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 
{ pkgs, lib, config, ... }:
{
  options = {
    services.thinkmorse = {
      enable = lib.mkEnableOption "Enable morse on the thinkpad led";
      message = lib.mkOption {
        type = lib.types.str;
        default = "Hello, World!";
        description = "The message to display in morse code";
      };
      devices = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ "tpacpi::lid_logo_dot" ];
        description = "The devices to use for morse code";
      };
      speed = lib.mkOption {
        type = lib.types.str;
        default = "0.1";
        description = "Duration of a dit in seconds";
      };
    };
  };
  config = lib.mkIf config.services.thinkmorse.enable (
    let
      ditDelay = config.services.thinkmorse.speed;
      thinkmorse = pkgs.writeShellScriptBin "thinkmorse" ''
        #!${pkgs.bash}/bin/bash

        modprobe -r ec_sys
        modprobe ec_sys write_support=1

        led(){
          ${lib.concatStringsSep "\n" (builtins.map (device: ''echo $1 | ${pkgs.coreutils}/bin/tee /sys/class/leds/${device}/brightness'' ) config.services.thinkmorse.devices )}
        }

        dit(){
                led 1
                sleep ${ditDelay}
                led 0
                sleep ${ditDelay}
        }

        dah(){
                led 1
                sleep ${ditDelay}
                sleep ${ditDelay}
                sleep ${ditDelay}
                led 0
                sleep ${ditDelay}
        }

        morse(){
                case $1 in
                        "0") dah; dah; dah; dah; dah;;
                        "1") dit; dah; dah; dah; dah;;
                        "2") dit; dit; dah; dah; dah;;
                        "3") dit; dit; dit; dah; dah;;
                        "4") dit; dit; dit; dit; dah;;
                        "5") dit; dit; dit; dit; dit;;
                        "6") dah; dit; dit; dit; dit;;
                        "7") dah; dah; dit; dit; dit;;
                        "8") dah; dah; dah; dit; dit;;
                        "9") dah; dah; dah; dah; dit;;
                        "a") dit; dah;;
                        "b") dah; dit; dit; dit;;
                        "c") dah; dit; dah; dit;;
                        "d") dah; dit; dit;;
                        "e") dit;;
                        "f") dit; dit; dah; dit;;
                        "g") dah; dah; dit;;
                        "h") dit; dit; dit; dit;;
                        "i") dit; dit;;
                        "j") dit; dah; dah; dah;;
                        "k") dah; dit; dah;;
                        "l") dit; dah; dit; dit;;
                        "m") dah; dah;;
                        "n") dah; dit;;
                        "o") dah; dah; dah;;
                        "p") dit; dah; dah; dit;;
                        "q") dah; dah; dit; dah;;
                        "r") dit; dah; dit;;
                        "s") dit; dit; dit;;
                        "t") dah;;
                        "u") dit; dit; dah;;
                        "v") dit; dit; dit; dah;;
                        "w") dit; dah; dah;;
                        "x") dah; dit; dit; dah;;
                        "y") dah; dit; dah; dah;;
                        "z") dah; dah; dit; dit;;
                        " ") sleep ${ditDelay}; sleep ${ditDelay}; sleep ${ditDelay}; sleep ${ditDelay}; sleep ${ditDelay}; sleep ${ditDelay} ;;
                        #*) echo "done";;
                esac
                sleep 0.2;
        }

        parse(){
                tmp=$1
                for i in $(seq 0 ''${#tmp})
                do
                        echo "current letter: ''${tmp:$i:1}"
                        morse ''${tmp:$i:1}
                done
        }
        led 0
        parse "${config.services.thinkmorse.message}"
        led 0
        sleep 1
      '';
    in
    {
      systemd.services.thinkmorse = {
        enable = true;
        description = "Morse a message on the thinkpad led";
        after = [ "systemd-modules-load.service" ];
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          Restart = "always";
          Type = "simple";
          ExecStart = "${lib.getExe thinkmorse}";
        };
      };

    }
  );
}