From e94d0227fede63f08ecbbcb271d1073be38c8ee3 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Tue, 1 Jun 2021 00:02:27 +0200 Subject: [PATCH] Use black --- flake.nix | 2 + pkgs/bwrap-helper/bwrap-helper.py | 208 +++++++++++++++++++----------- 2 files changed, 134 insertions(+), 76 deletions(-) diff --git a/flake.nix b/flake.nix index 98e0ec9..ec691f9 100644 --- a/flake.nix +++ b/flake.nix @@ -62,6 +62,7 @@ pre-commit-check = nix-pre-commit-hooks.lib.${system}.run { src = ./.; hooks = { + black.enable = true; nixpkgs-fmt.enable = true; shellcheck.enable = true; }; @@ -138,6 +139,7 @@ devShell = pkgs.mkShell { buildInputs = (with pkgs; [ + black nixpkgs-fmt shellcheck sops diff --git a/pkgs/bwrap-helper/bwrap-helper.py b/pkgs/bwrap-helper/bwrap-helper.py index 71e6fa0..92388f1 100755 --- a/pkgs/bwrap-helper/bwrap-helper.py +++ b/pkgs/bwrap-helper/bwrap-helper.py @@ -15,7 +15,9 @@ def add_switch(name: str, default=False): if default: parser.add_argument(f"--no-{name}", dest=name, action="store_false") else: - parser.add_argument(f"--{name}", dest=name, action="store_true", default=default) + parser.add_argument( + f"--{name}", dest=name, action="store_true", default=default + ) def tmp_file(name: str): @@ -76,82 +78,136 @@ path_entries = [ ] argument_groups = { - "base": (True, [ - "--tmpfs", "/tmp", - "--proc", "/proc", - "--dev", "/dev", - "--dir", home, - "--dir", f"/run/user/{uid}", - *ro_bind("/etc/localtime"), - "--unshare-all", - "--die-with-parent", - ]), - "nix-store": (True, [ - *flat_map(ro_bind, [ - "/nix/store", - "/etc/static", - ]), - ]), - "path": (True, [ - *flat_map(ro_bind_try, path_entries), - *setenv("PATH", ":".join(path_entries)), - *ro_bind_try("/run/current-system/sw") # not really path, but also libraries etc. - ]), - "gui": (False, [ - *dev_bind("/dev/dri"), - *flat_map(ro_bind, [ - "/sys/dev/char", - "/sys/devices/pci0000:00", - f"/run/user/{uid}/{os.getenv('WAYLAND_DISPLAY')}", - "/run/opengl-driver", - "/etc/fonts", - ]), - *ro_bind_try("/run/opengl-driver-32"), - ]), - "x11": (False, [ - *ro_bind("/tmp/.X11-unix"), - ]), - "audio": (False, [ - *ro_bind(f"/run/user/{uid}/pulse"), - # should in theory autodetect, but sometimes it does not work - *setenv("PULSE_SERVER", f"/run/user/{uid}/pulse/native"), - # some programs need the cookie - *ro_bind(f"{home}/.config/pulse/cookie"), - *setenv("PULSE_COOKIE", f"{home}/.config/pulse/cookie"), - # ALSA compat - *ro_bind("/etc/asound.conf"), - ]), - "passwd": (False, [ - *ro_bind( - generate_tmp_file( - "passwd", - f"{username}:x:{uid}:{gid}::{home}:/run/current-system/sw/bin/bash\n" + "base": ( + True, + [ + "--tmpfs", + "/tmp", + "--proc", + "/proc", + "--dev", + "/dev", + "--dir", + home, + "--dir", + f"/run/user/{uid}", + *ro_bind("/etc/localtime"), + "--unshare-all", + "--die-with-parent", + ], + ), + "nix-store": ( + True, + [ + *flat_map( + ro_bind, + [ + "/nix/store", + "/etc/static", + ], ), - "/etc/passwd" - ) - ]), - "network": (False, [ - "--share-net", - *flat_map(ro_bind, [ - "/etc/resolv.conf", - "/etc/ssl/certs", - ]), - ]), - "dbus": (False, [ - *ro_bind("/run/dbus/system_bus_socket"), - *ro_bind(generate_tmp_file("machine-id", "0" * 32), "/etc/machine-id"), - ]), - "new-session": (True, [ - "--new-session", - ]), - "pwd": (False, [ - *ro_bind(os.getcwd()), - "--chdir", os.getcwd(), - ]), - "pwd-rw": (False, [ - *bind(os.getcwd()), - "--chdir", os.getcwd(), - ]), + ], + ), + "path": ( + True, + [ + *flat_map(ro_bind_try, path_entries), + *setenv("PATH", ":".join(path_entries)), + *ro_bind_try( + "/run/current-system/sw" + ), # not really path, but also libraries etc. + ], + ), + "gui": ( + False, + [ + *dev_bind("/dev/dri"), + *flat_map( + ro_bind, + [ + "/sys/dev/char", + "/sys/devices/pci0000:00", + f"/run/user/{uid}/{os.getenv('WAYLAND_DISPLAY')}", + "/run/opengl-driver", + "/etc/fonts", + ], + ), + *ro_bind_try("/run/opengl-driver-32"), + ], + ), + "x11": ( + False, + [ + *ro_bind("/tmp/.X11-unix"), + ], + ), + "audio": ( + False, + [ + *ro_bind(f"/run/user/{uid}/pulse"), + # should in theory autodetect, but sometimes it does not work + *setenv("PULSE_SERVER", f"/run/user/{uid}/pulse/native"), + # some programs need the cookie + *ro_bind(f"{home}/.config/pulse/cookie"), + *setenv("PULSE_COOKIE", f"{home}/.config/pulse/cookie"), + # ALSA compat + *ro_bind("/etc/asound.conf"), + ], + ), + "passwd": ( + False, + [ + *ro_bind( + generate_tmp_file( + "passwd", + f"{username}:x:{uid}:{gid}::{home}:/run/current-system/sw/bin/bash\n", + ), + "/etc/passwd", + ) + ], + ), + "network": ( + False, + [ + "--share-net", + *flat_map( + ro_bind, + [ + "/etc/resolv.conf", + "/etc/ssl/certs", + ], + ), + ], + ), + "dbus": ( + False, + [ + *ro_bind("/run/dbus/system_bus_socket"), + *ro_bind(generate_tmp_file("machine-id", "0" * 32), "/etc/machine-id"), + ], + ), + "new-session": ( + True, + [ + "--new-session", + ], + ), + "pwd": ( + False, + [ + *ro_bind(os.getcwd()), + "--chdir", + os.getcwd(), + ], + ), + "pwd-rw": ( + False, + [ + *bind(os.getcwd()), + "--chdir", + os.getcwd(), + ], + ), } passthrough_args = [