fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
3.4 KiB134 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.fusuma;
8
9  yamlFormat = pkgs.formats.yaml { };
10
11  configJson = pkgs.writeText "config.json" (builtins.toJSON cfg.settings);
12
13  configYamlRaw = pkgs.runCommand "config.yaml.raw" { } ''
14    ${pkgs.remarshal}/bin/json2yaml -i ${configJson} -o $out;
15  '';
16
17  # convert keys into literal numbers where necessary,
18  # fusuma does not support string type finger count.
19  strToInt = pkgs.writers.writePython3 "str2int" {
20    libraries = [ pkgs.python3Packages.pyyaml ];
21  } ''
22    import yaml
23    from yaml import FullLoader
24
25
26    def str2int(config):
27        if type(config) is not dict:
28            return
29
30        for key in list(config):
31            if type(config[key]) is dict and key.isdigit():
32                t = config[key]
33                del config[key]
34                config[int(key)] = t
35            else:
36                str2int(config[key])
37
38
39    if __name__ == '__main__':
40        path = "${configYamlRaw}"
41        with open(path) as f:
42            config = yaml.load(f, Loader=FullLoader)
43            str2int(config)
44            print(yaml.dump(config))
45  '';
46
47  configYaml = pkgs.stdenv.mkDerivation {
48    name = "config.yaml";
49    buildCommand = ''
50      ${strToInt} > $out
51    '';
52  };
53
54  makeBinPath = packages:
55    foldl (a: b: if a == "" then b else "${a}:${b}") ""
56    (map (pkg: "${pkg}/bin") packages);
57
58in {
59  meta.maintainers = [ hm.maintainers.iosmanthus ];
60
61  options.services.fusuma = {
62    enable = mkEnableOption
63      "the fusuma systemd service to automatically enable touchpad gesture";
64
65    package = mkOption {
66      type = types.package;
67      default = pkgs.fusuma;
68      defaultText = literalExpression "pkgs.fusuma";
69      description = "Package providing {command}`fusuma`.";
70    };
71
72    settings = mkOption {
73      type = yamlFormat.type;
74      example = literalExpression ''
75        {
76          threshold = {
77            swipe = 0.1;
78          };
79          interval = {
80            swipe = 0.7;
81          };
82          swipe = {
83            "3" = {
84              left = {
85                # GNOME: Switch to left workspace
86                command = "xdotool key ctrl+alt+Right";
87              };
88            };
89          };
90        };
91      '';
92      description = ''
93        YAML config that will override the default fusuma configuration.
94      '';
95    };
96
97    extraPackages = mkOption {
98      type = types.listOf types.package;
99      default = with pkgs; [ coreutils ];
100      defaultText = literalExpression "pkgs.coreutils";
101      example = literalExpression ''
102        with pkgs; [ coreutils xdotool ];
103      '';
104      description = ''
105        Extra packages needs to bring to the scope of fusuma service.
106      '';
107    };
108  };
109
110  config = mkIf cfg.enable {
111    assertions = [
112      (lib.hm.assertions.assertPlatform "services.fusuma" pkgs
113        lib.platforms.linux)
114    ];
115
116    xdg.configFile."fusuma/config.yaml".source = configYaml;
117
118    systemd.user.services.fusuma = {
119      Unit = {
120        Description = "Fusuma services";
121        After = [ "graphical-session-pre.target" ];
122        PartOf = [ "graphical-session.target" ];
123      };
124
125      Service = {
126        Environment = with pkgs; "PATH=${makeBinPath cfg.extraPackages}";
127        ExecStart =
128          "${cfg.package}/bin/fusuma -c ${config.xdg.configHome}/fusuma/config.yaml";
129      };
130
131      Install = { WantedBy = [ "graphical-session.target" ]; };
132    };
133  };
134}