This repository has no description
6.4 kB
206 lines
1{ self, ... }:
2{
3 flake.wrapperModules.workspace-manager =
4 {
5 config,
6 lib,
7 pkgs,
8 ...
9 }:
10 let
11 cfg = config.lass.workspace-manager;
12
13 workspaceType = lib.types.submodule {
14 options = {
15 directory = lib.mkOption {
16 type = lib.types.str;
17 description = "Directory to use for this workspace";
18 };
19 };
20 };
21
22 # Generate a directory with JSON files for each declared workspace
23 systemConfigDir = pkgs.linkFarm "workspace-manager-configs" (
24 lib.mapAttrsToList (name: ws: {
25 name = "${name}.json";
26 path = pkgs.writeText "${name}.json" (builtins.toJSON { inherit (ws) directory; });
27 }) cfg.workspaces
28 );
29 in
30 {
31 options.lass.workspace-manager = {
32 enable = lib.mkEnableOption "workspace manager daemon for sway";
33
34 package = lib.mkOption {
35 type = lib.types.package;
36 default = self.packages.${pkgs.system}.workspace-manager-daemon;
37 description = "The workspace-manager-daemon package to use";
38 };
39
40 workspaces = lib.mkOption {
41 type = lib.types.attrsOf workspaceType;
42 default = { };
43 description = "Declarative workspace configurations (take precedence over user configs)";
44 example = lib.literalExpression ''
45 {
46 dev = { directory = "/home/user/src/myproject"; };
47 music = { directory = "/home/user/Music"; };
48 }
49 '';
50 };
51
52 configDir = lib.mkOption {
53 type = lib.types.str;
54 default = "~/workspaces";
55 description = "Directory containing user workspace configuration files";
56 };
57
58 defaultDirectory = lib.mkOption {
59 type = lib.types.str;
60 default = "~";
61 description = "Default directory for workspaces without configuration";
62 };
63 };
64
65 config = lib.mkIf cfg.enable {
66 systemd.user.services.workspace-manager = {
67 description = "Workspace Manager Daemon";
68 partOf = [ "sway-session.target" ];
69 wantedBy = [ "sway-session.target" ];
70 serviceConfig = {
71 Type = "simple";
72 ExecStart = "${cfg.package}/bin/workspace-manager-daemon";
73 Restart = "on-failure";
74 RestartSec = 5;
75 Environment = [
76 "WORKSPACE_MANAGER_CONFIG_DIR=${cfg.configDir}"
77 "WORKSPACE_MANAGER_SYSTEM_CONFIG_DIR=${systemConfigDir}"
78 "WORKSPACE_MANAGER_DEFAULT_DIR=${cfg.defaultDirectory}"
79 ];
80 };
81 };
82 };
83 };
84
85 perSystem =
86 {
87 pkgs,
88 ...
89 }:
90 let
91 python = pkgs.python3;
92 in
93 {
94 packages.workspace-manager =
95 (pkgs.writeShellApplication {
96 name = "workspace-manager";
97 runtimeInputs = [
98 pkgs.socat
99 pkgs.jq
100 ];
101 text = builtins.readFile ./workspace-manager.sh;
102 }).overrideAttrs
103 { passthru.usage = builtins.readFile ./usage.kdl; };
104
105 packages.workspace-menu = pkgs.writeShellApplication {
106 name = "workspace-menu";
107 runtimeInputs = [
108 pkgs.sway
109 pkgs.jq
110 self.packages.${pkgs.system}.menu
111 pkgs.libnotify
112 pkgs.findutils
113 pkgs.gnused
114 pkgs.coreutils
115 pkgs.gnugrep
116 pkgs.firefox
117 ];
118 text = builtins.readFile ./workspace-menu.sh;
119 };
120
121 packages.workspace-manager-daemon = pkgs.stdenv.mkDerivation {
122 pname = "workspace-manager-daemon";
123 version = "0.1.0";
124 src = ./.;
125 nativeBuildInputs = [
126 pkgs.makeWrapper
127 pkgs.ruff
128 python.pkgs.mypy
129 ];
130 doCheck = true;
131 checkPhase = ''
132 runHook preCheck
133 ruff check daemon.py
134 ruff format --check daemon.py
135 mypy --strict daemon.py
136 runHook postCheck
137 '';
138 installPhase = ''
139 runHook preInstall
140 mkdir -p $out/bin
141 cp daemon.py $out/bin/workspace-manager-daemon
142 chmod +x $out/bin/workspace-manager-daemon
143 wrapProgram $out/bin/workspace-manager-daemon \
144 --prefix PATH : ${python}/bin:${pkgs.sway}/bin
145 runHook postInstall
146 '';
147 meta.mainProgram = "workspace-manager-daemon";
148 };
149
150 # Open Firefox for the current workspace
151 packages.workspace-browser = pkgs.writeShellScriptBin "workspace-browser" ''
152 exec firefox "$@"
153 '';
154
155 # Native messaging host for the workspace-tabs Firefox extension
156 packages.workspace-tabs-native-host = pkgs.stdenv.mkDerivation {
157 pname = "workspace-tabs-native-host";
158 version = "0.1.0";
159 src = ./.;
160 nativeBuildInputs = [
161 pkgs.makeWrapper
162 pkgs.ruff
163 python.pkgs.mypy
164 ];
165 doCheck = true;
166 checkPhase = ''
167 runHook preCheck
168 ruff check workspace-tabs-host.py
169 ruff format --check workspace-tabs-host.py
170 mypy --strict workspace-tabs-host.py
171 runHook postCheck
172 '';
173 installPhase = ''
174 runHook preInstall
175 mkdir -p $out/bin $out/lib/mozilla/native-messaging-hosts
176 cp workspace-tabs-host.py $out/bin/workspace-tabs-host
177 chmod +x $out/bin/workspace-tabs-host
178 wrapProgram $out/bin/workspace-tabs-host \
179 --prefix PATH : ${python}/bin:${pkgs.sway}/bin
180
181 cat > $out/lib/mozilla/native-messaging-hosts/workspace_tabs.json <<EOF
182 {
183 "name": "workspace_tabs",
184 "description": "Native messaging host for Workspace Tabs extension",
185 "path": "$out/bin/workspace-tabs-host",
186 "type": "stdio",
187 "allowed_extensions": ["workspace-tabs@workspace-manager"]
188 }
189 EOF
190 runHook postInstall
191 '';
192 };
193
194 # Firefox extension for workspace tab management (packaged as .xpi)
195 packages.workspace-tabs-extension =
196 pkgs.runCommand "workspace-tabs-extension"
197 {
198 nativeBuildInputs = [ pkgs.zip ];
199 }
200 ''
201 mkdir -p $out
202 cd ${./firefox-extension}
203 zip -r "$out/workspace-tabs@workspace-manager.xpi" ./*
204 '';
205 };
206}