]> git.scottworley.com Git - nix-env-apps/blob - apps.py
4b0d0d581a5c917d442baf3f64432c9b1b37c36c
[nix-env-apps] / apps.py
1 from gi.repository import Gtk
2 import os
3 import subprocess
4
5 import gi
6 gi.require_version("Gtk", "4.0")
7
8
9 def on_edit(_):
10 config_dir = os.path.join(
11 os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')),
12 'nixpkgs',
13 'overlays')
14 os.makedirs(config_dir, exist_ok=True)
15 config_file = os.path.join(config_dir, 'userPackages.nix')
16 try:
17 with open(config_file, mode="x") as f:
18 f.write('''final: prev: {
19 userPackages = final.buildEnv {
20 name = "userPackages";
21 paths = (with final; [
22
23
24
25 ];
26 };
27 }
28 ''')
29 except FileExistsError:
30 pass
31 subprocess.run(['xdg-open', config_file], check=True)
32
33
34 def try_exec_terminal(terminal, args):
35 try:
36 os.execvp(terminal, [terminal] + args)
37 except FileNotFoundError:
38 pass
39
40
41 def on_apply(_):
42 command = ['nix-env', '-riA', 'nixos.userPackages']
43 command_string = ' '.join(command)
44 close_string = ''' && read -p "SUCCESS: Press ENTER to close this window" || read -p "FAILURE: Press ENTER to close this window"'''
45 # This should be a simple `xdg-terminal` invocation, but as of 2025,
46 # xdg-terminal is extremely broken in Gnome:
47 # * It doesn't cause a terminal window to appear
48 # * It doesn't run the given command
49 # * It exits with status 0, so you can't even tell that it failed
50 # * There's no way for the user to configure their preferred terminal?
51 # So we just try launching a whole bunch of terminals until we find one
52 # that works
53 if os.fork() == 0:
54 try_exec_terminal('kitty', ['sh', '-c', command_string + close_string])
55 try_exec_terminal(
56 'alactritty', [
57 '-e', 'sh', '-c', command_string + close_string])
58 try_exec_terminal('kgx', ['--'] + command)
59 try_exec_terminal(
60 'konsole', [
61 '-e', 'sh', '-c', command_string + close_string])
62 try_exec_terminal('xfce4-terminal', ['--hold', '-e', command_string])
63 try_exec_terminal('st', ['sh', '-c', command_string + close_string])
64 try_exec_terminal(
65 'urxvt', [
66 '-e', 'sh', '-c', command_string + close_string])
67 try_exec_terminal(
68 'xterm', [
69 '-e', 'sh', '-c', command_string + close_string])
70
71 # Don't even try gnome-terminal. Sometimes it will start, but not run the command,
72 # and exit with status 0 so we wouldn't even be able to tell that there was a problem.
73 # try_exec_terminal('gnome-terminal', ['--', 'sh', '-c', command_string + close_string])
74
75 # As a last resort, run the command directly, without a terminal, where
76 # the user probably can't see it. :(
77 os.execvp(command[0], command)
78
79
80 def on_activate(app):
81 win = Gtk.ApplicationWindow(application=app)
82 box = Gtk.Box()
83 box.set_orientation(Gtk.Orientation.VERTICAL)
84 edit = Gtk.Button(label="Edit Configuration")
85 apply = Gtk.Button(label="Apply Configuration")
86 edit.connect('clicked', on_edit)
87 apply.connect('clicked', on_apply)
88 box.append(edit)
89 box.append(apply)
90 win.set_child(box)
91 win.present()
92
93
94 def main():
95 app = Gtk.Application(application_id='net.chkno.nix-env-apps')
96 app.connect('activate', on_activate)
97 app.run(None)
98
99
100 if __name__ == '__main__':
101 main()