diff options
Diffstat (limited to 'lass/3modules')
-rw-r--r-- | lass/3modules/autowifi.nix | 95 | ||||
-rw-r--r-- | lass/3modules/browsers.nix | 87 | ||||
-rw-r--r-- | lass/3modules/default.nix | 1 |
3 files changed, 99 insertions, 84 deletions
diff --git a/lass/3modules/autowifi.nix b/lass/3modules/autowifi.nix index 930d99727..9aa1a2d28 100644 --- a/lass/3modules/autowifi.nix +++ b/lass/3modules/autowifi.nix @@ -11,101 +11,28 @@ in { type = types.str; default = "/etc/wifis"; }; + enablePrisonBreak = mkOption { + type = types.bool; + default = false; + }; }; - config = { + config = lib.mkIf cfg.enable { systemd.services.autowifi = { description = "Automatic wifi connector"; wantedBy = [ "multi-user.target" ]; + path = [ pkgs.networkmanager ]; serviceConfig = { Type = "simple"; Restart = "always"; RestartSec = "10s"; - ExecStart = pkgs.writers.writePython3 "autowifi" {} /* python3 */ '' - import subprocess - import time - import urllib.request - - - def connect(ssid, psk=None): - subprocess.run(["${pkgs.networkmanager}/bin/nmcli", "connection", "delete", "autowifi"]) - print("connecting to {}".format(ssid)) - if psk is None: - subprocess.run(["${pkgs.networkmanager}/bin/nmcli", "device", "wifi", "connect", ssid, "name", "autowifi"]) - else: - subprocess.run(["${pkgs.networkmanager}/bin/nmcli", "device", "wifi", "connect", ssid, "name", "autowifi", "password", psk]) - - - def scan(): - wifis_raw = subprocess.check_output(["${pkgs.networkmanager}/bin/nmcli", "-t", "device", "wifi", "list", "--rescan", "yes"]) - wifis_list = wifis_raw.split(b'\n') - wifis = [] - for line in wifis_list: - ls = line.split(b':') - if len(ls) == 8: - wifis.append({"ssid": ls[1], "signal": int(ls[5]), "crypto": ls[7]}) - return wifis - - - def get_known_wifis(): - wifis_lines = [] - with open('${cfg.knownWifisFile}') as f: - wifis_lines = f.read().splitlines() - wifis = [] - for line in wifis_lines: - ls = line.split(':') - wifis.append({"ssid": ls[0].encode(), "psk": ls[1].encode()}) - return wifis - - - def check_internet(): - try: - beacon = urllib.request.urlopen('http://krebsco.de/secret') - except: # noqa - print("no internet") - return False - if beacon.read() == b'1337\n': - return True - print("no internet") - return False - - - def is_wifi_open(wifi): - if wifi['crypto'] == ${"b''"}: - return True - else: - return False - - - def is_wifi_seen(wifi, seen_wifis): - for seen_wifi in seen_wifis: - if seen_wifi["ssid"] == wifi["ssid"]: - return True - return False - - - def bloop(): - while True: - if not check_internet(): - wifis = scan() - known_wifis = get_known_wifis() - known_seen_wifis = [wifi for wifi in known_wifis if is_wifi_seen(wifi, wifis)] - for wifi in known_seen_wifis: - connect(wifi['ssid'], wifi['psk']) - if check_internet(): - continue - open_wifis = filter(is_wifi_open, wifis) - for wifi in open_wifis: - connect(wifi['ssid']) - if check_internet(): - continue - time.sleep(10) - - - bloop() - ''; + ExecStart = "${autowifi}/bin/autowifi"; }; }; + + networking.networkmanager.dispatcherScripts = mkIf cfg.enablePrisonBreak [ + { source = "${pkgs.callPackage <stockholm/makefu/5pkgs/prison-break}/bin/prison-break"; } + ]; }; } diff --git a/lass/3modules/browsers.nix b/lass/3modules/browsers.nix new file mode 100644 index 000000000..ccb108f8a --- /dev/null +++ b/lass/3modules/browsers.nix @@ -0,0 +1,87 @@ +{ config, lib, pkgs, ... }: +with import <stockholm/lib>; +let + + cfg = config.lass.browser; + + browserScripts = { + chromium = "${pkgs.chromium}/bin/chromium"; + firefox = "${pkgs.firefox.override { + extraNativeMessagingHosts = [ pkgs.tridactyl-native ]; + }}/bin/firefox"; + qutebrowser = "${pkgs.qutebrowser}/bin/qutebrowser"; + }; + + browser-select = let + sortedPaths = sort (a: b: a.value.precedence > b.value.precedence) + (mapAttrsToList (name: value: { inherit name value; }) + cfg.config); + in if (lib.length sortedPaths) > 1 then + pkgs.writeScriptBin "browser-select" '' + BROWSER=$(echo -e "${concatStringsSep "\\n" (map (getAttr "name") sortedPaths)}" | ${pkgs.dmenu}/bin/dmenu) + case $BROWSER in + ${concatMapStringsSep "\n" (n: '' + ${n.name}) + export BIN=${config.lass.xjail-bins.${n.name}}/bin/${n.name} + ;; + '') (sortedPaths)} + esac + $BIN "$@" + '' + else + let + name = (lib.head sortedPaths).name; + in pkgs.writeScriptBin "browser-select2" '' + ${config.lass.xjail-bins.${name}}/bin/${name} "$@" + '' + ; + +in { + options.lass.browser = { + select = mkOption { + type = types.path; + }; + config = mkOption { + type = types.attrsOf (types.submodule ({ config, ... }: { + options = { + name = mkOption { + type = types.str; + default = config._module.args.name; + }; + precedence = mkOption { + type = types.int; + default = 0; + }; + user = mkOption { + type = types.str; + default = config._module.args.name; + }; + browser = mkOption { + type = types.enum (attrNames browserScripts); + default = "chromium"; + }; + groups = mkOption { + type = types.listOf types.str; + default = []; + }; + }; + })); + default = {}; + }; + }; + + config = (mkIf (cfg.config != {}) { + lass.xjail = mapAttrs' (name: browser: + nameValuePair name { + script = browserScripts.${browser.browser}; + groups = browser.groups; + } + ) cfg.config; + environment.systemPackages = (map (browser: + config.lass.xjail-bins.${browser.name} + ) (attrValues cfg.config)) ++ [ + browser-select + ]; + lass.browser.select = browser-select; + }); +} diff --git a/lass/3modules/default.nix b/lass/3modules/default.nix index 1195cd3d4..90dcb9d9c 100644 --- a/lass/3modules/default.nix +++ b/lass/3modules/default.nix @@ -15,5 +15,6 @@ _: ./usershadow.nix ./xjail.nix ./autowifi.nix + ./browsers.nix ]; } |