]>
git.scottworley.com Git - nix-env-apps/blob - apps.py
3 from typing
import Any
, Callable
6 gi
.require_version("Gtk", "4.0")
7 from gi
.repository
import Gtk
# nopep8 pylint: disable=wrong-import-position
10 def on_edit(_
: Any
) -> None:
11 config_dir
= os
.path
.join(
12 os
.environ
.get('XDG_CONFIG_HOME', os
.path
.expanduser('~/.config')),
15 os
.makedirs(config_dir
, exist_ok
=True)
16 config_file
= os
.path
.join(config_dir
, 'userPackages.nix')
18 with open(config_file
, mode
="x", encoding
='utf-8') as f
:
19 f
.write('''final: prev: {
20 userPackages = final.buildEnv {
21 name = "userPackages";
22 paths = (with final; [
30 except FileExistsError
:
32 subprocess
.run(['xdg-open', config_file
], check
=True)
35 def try_exec_terminal(terminal
: str, args
: list[str]) -> None:
37 os
.execvp(terminal
, [terminal
] + args
)
38 except FileNotFoundError
:
42 def on_apply(_
: Any
) -> None:
43 command
= ['nix-env', '-riA', 'nixos.userPackages']
44 command_string
= ' '.join(command
)
46 ''' && read -p "SUCCESS: Press ENTER to close this window"''' +
47 ''' || read -p "FAILURE: Press ENTER to close this window"''')
48 # This should be a simple `xdg-terminal` invocation, but as of 2025,
49 # xdg-terminal is extremely broken in Gnome:
50 # * It doesn't cause a terminal window to appear
51 # * It doesn't run the given command
52 # * It exits with status 0, so you can't even tell that it failed
53 # * There's no way for the user to configure their preferred terminal?
54 # So we just try launching a whole bunch of terminals until we find one
57 try_exec_terminal('kitty', ['sh', '-c', command_string
+ close_string
])
60 '-e', 'sh', '-c', command_string
+ close_string
])
61 try_exec_terminal('kgx', ['--'] + command
)
64 '-e', 'sh', '-c', command_string
+ close_string
])
65 try_exec_terminal('xfce4-terminal', ['--hold', '-e', command_string
])
66 try_exec_terminal('st', ['sh', '-c', command_string
+ close_string
])
69 '-e', 'sh', '-c', command_string
+ close_string
])
72 '-e', 'sh', '-c', command_string
+ close_string
])
74 # Don't even try gnome-terminal. Sometimes it will start, but not run the command,
75 # and exit with status 0 so we wouldn't even be able to tell that there was a problem.
76 # try_exec_terminal('gnome-terminal', ['--', 'sh', '-c', command_string + close_string])
78 # As a last resort, run the command directly, without a terminal, where
79 # the user probably can't see it. :(
80 os
.execvp(command
[0], command
)
83 def make_button(label
: str, action
: Callable
[[Any
], None]) -> Gtk
.Button
:
84 button
= Gtk
.Button(label
=label
)
85 button
.connect('clicked', action
)
86 button
.set_margin_top(10)
87 button
.set_margin_start(10)
88 button
.set_margin_end(10)
89 button
.set_margin_bottom(10)
93 def on_activate(app
: Gtk
.Application
) -> None:
94 win
= Gtk
.ApplicationWindow(application
=app
)
97 box
.set_orientation(Gtk
.Orientation
.VERTICAL
)
98 edit
= make_button("Edit Configuration", on_edit
)
99 apply = make_button("Apply Configuration", on_apply
)
107 app
= Gtk
.Application(application_id
='net.chkno.nix-env-apps')
108 app
.connect('activate', on_activate
)
112 if __name__
== '__main__':