diff --git a/hosts/pinwheel/modules/waybar/default.nix b/hosts/pinwheel/modules/waybar/default.nix index e232177..21109b0 100644 --- a/hosts/pinwheel/modules/waybar/default.nix +++ b/hosts/pinwheel/modules/waybar/default.nix @@ -87,26 +87,33 @@ let esac ''; - work-vpn-status = pkgs.writeShellScript "work-vpn-status" '' - ACTIVE_ENVS="" + work-vpn = pkgs.writeShellScript "work-vpn" '' + connected() { + systemctl is-active --quiet openvpn-work-staging.service openvpn-work-production.service + } - STAGING_STATUS=$(systemctl is-active openvpn-work-staging.service) - if [ "$STAGING_STATUS" == "active" ]; then - ACTIVE_ENVS="S" - fi + output() { + if connected; then + echo '{ "text": "WORK-VPN ON", "class": "connected" }' + else + echo "{ \"text\": \"󰖂\", \"tooltip\": \"Work VPN off\", \"class\": \"disconnected\" }" + fi + } - PRODUCTION_STATUS=$(systemctl is-active openvpn-work-production.service) - if [ "$PRODUCTION_STATUS" == "active" ]; then - if [ -n "$ACTIVE_ENVS" ]; then - ACTIVE_ENVS="$ACTIVE_ENVS&P" - else - ACTIVE_ENVS="P" - fi - fi + toggle() { + if connected; then + systemctl stop work-vpn.target && ${pkgs.libnotify}/bin/notify-send "Disconnected from work VPN" + else + systemctl start work-vpn.target && ${pkgs.libnotify}/bin/notify-send "Connected to work VPN" + fi + } - if [ -n "$ACTIVE_ENVS" ]; then - echo "WORK-VPN $ACTIVE_ENVS ON" - fi + case $1 in + --toggle) + toggle ;; + --output) + output ;; + esac ''; toggle-bt-power = pkgs.writeShellScript "toggle-bt-power" '' @@ -142,20 +149,22 @@ in modules-left = lib.mkIf hyprlandEnabled [ "hyprland/workspaces" ]; modules-right = [ - "custom/work-vpn-status" "custom/music" "custom/container-status" "custom/dunst" "bluetooth" "wireplumber" + "custom/work-vpn" "custom/tailscale" "network" "battery" "clock" ]; - "custom/work-vpn-status" = { - exec = "${work-vpn-status}"; + "custom/work-vpn" = { + exec = "${work-vpn} --output"; + return-type = "json"; + on-click-right = "${work-vpn} --toggle"; interval = 2; }; @@ -247,12 +256,14 @@ in modules-left = lib.mkIf hyprlandEnabled [ "hyprland/workspaces" ]; modules-right = [ - "custom/work-vpn-status" + "custom/work-vpn" "clock" ]; - "custom/work-vpn-status" = { - exec = "${work-vpn-status}"; + "custom/work-vpn" = { + exec = "${work-vpn} --output"; + return-type = "json"; + on-click-right = "${work-vpn} --toggle"; interval = 2; }; @@ -306,10 +317,14 @@ in color: #${config.lib.colors.warning}; } - #custom-work-vpn-status { + #custom-work-vpn.connected { color: #${config.lib.colors.warning}; } + #custom-work-vpn.disconnected { + color: #${config.lib.colors.foreground-dim}; + } + #custom-container-status.running { color: #${config.lib.colors.warning}; font-size: 30px; diff --git a/hosts/pinwheel/modules/work/default.nix b/hosts/pinwheel/modules/work/default.nix index 10cb83f..83be827 100644 --- a/hosts/pinwheel/modules/work/default.nix +++ b/hosts/pinwheel/modules/work/default.nix @@ -91,10 +91,10 @@ EOF (pkgs.writeShellScriptBin "work-vpn" '' case $1 in up) - sudo sh -c "systemctl start openvpn-work-staging.service; systemctl start openvpn-work-production.service" + systemctl start work-vpn.target ;; down) - sudo sh -c "systemctl stop openvpn-work-staging.service; systemctl stop openvpn-work-production.service" + systemctl stop work-vpn.target ;; esac '') @@ -142,6 +142,34 @@ EOF }; }; + systemd.targets.work-vpn = lib.mkIf openvpnEnabled { + description = "Work VPN"; + requires = [ + "openvpn-work-staging.service" + "openvpn-work-production.service" + ]; + after = [ + "openvpn-work-staging.service" + "openvpn-work-production.service" + ]; + }; + + systemd.services = lib.mkIf openvpnEnabled { + openvpn-work-staging.partOf = [ "work-vpn.target" ]; + openvpn-work-production.partOf = [ "work-vpn.target" ]; + }; + + security.polkit.extraConfig = lib.mkIf openvpnEnabled '' + polkit.addRule(function(action, subject) { + if (action.id == "org.freedesktop.systemd1.manage-units" && + subject.user == "alex" && + action.lookup("unit") == "work-vpn.target" && + ["start", "stop", "restart"].indexOf(action.lookup("verb")) >= 0) { + return polkit.Result.YES; + } + }); + ''; + age.secrets = { "work-gitconfig" = lib.mkIf gitEnabled { file = ../../../../secrets/pinwheel/work-gitconfig.age;