{ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; flake-utils.url = "github:numtide/flake-utils"; naersk = { url = "github:nix-community/naersk"; inputs.nixpkgs.follows = "nixpkgs"; }; rust-overlay = { url = "github:oxalica/rust-overlay"; inputs.nixpkgs.follows = "nixpkgs"; }; }; outputs = { self, flake-utils, naersk, nixpkgs, rust-overlay, }: let mkPackageSet = pkgs: let inherit (pkgs) lib; cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); pname = "globalprotect-openconnect"; version = cargoToml.workspace.package.version; releaseTag = "v2.6.5"; releaseVersion = "2.6.5"; toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; naersk' = pkgs.callPackage naersk { cargo = toolchain; rustc = toolchain; }; unpackReleaseAsset = { name, archive }: pkgs.runCommand name { nativeBuildInputs = [ pkgs.libarchive ]; } '' mkdir -p "$out" bsdtar --extract --file ${archive} --directory "$out" --strip-components 1 ''; src = lib.cleanSourceWith { src = lib.cleanSource ./.; filter = path: type: !(builtins.elem (builtins.baseNameOf path) [ "target" ".build" "node_modules" ]); }; cpu = pkgs.stdenv.hostPlatform.parsed.cpu.name; gpguiHashes = { x86_64 = "sha256-uVn3hJkKrjKz3mzRamxFu+0rE7OjLtMlT16d14XPcq4="; aarch64 = "sha256-g4HKQwFAM4YCP+Afew1AhzC1wPrCS8ICw3GeA7KIeFU="; }; gpguiArchive = pkgs.fetchurl { url = "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/${releaseTag}/gpgui_${cpu}.bin.tar.xz"; hash = gpguiHashes.${cpu}; }; gpgui = unpackReleaseAsset { name = "gpgui-${releaseVersion}-${cpu}"; archive = gpguiArchive; }; binaryHashes = { x86_64 = "sha256-jgHZC00q+2GnO18/aPGONDCz2CeiF/e1DucefSacv2w="; aarch64 = "sha256-FDCZN0bmKWKjPiF9wkRaqMUu0PfSVHDUveJVFts25qg="; }; binaryArchive = pkgs.fetchurl { url = "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/${releaseTag}/globalprotect-openconnect_${releaseVersion}_${cpu}.bin.tar.xz"; hash = binaryHashes.${cpu}; }; binaryPackage = unpackReleaseAsset { name = "globalprotect-openconnect-${releaseVersion}-${cpu}-binary"; archive = binaryArchive; }; linuxRuntimeDependencies = with pkgs; [ glib-networking libayatana-appindicator xdg-utils ]; rewriteVpncScriptToolPaths = lib.optionalString pkgs.stdenv.isLinux '' substituteInPlace $out/libexec/gpclient/vpnc-script \ --replace-fail /usr/bin/resolvectl ${pkgs.systemd}/bin/resolvectl \ --replace-fail /usr/bin/busctl ${pkgs.systemd}/bin/busctl ''; linuxBuildInputs = with pkgs; [ libxml2 zlib lz4 gnutls p11-kit nettle gmp ] ++ lib.optionals stdenv.isLinux [ glib gtk3 libsoup_3 webkitgtk_4_1 glib-networking openssl libsecret libayatana-appindicator ]; rewriteSourceInstallPaths = lib.optionalString pkgs.stdenv.isLinux '' substituteInPlace $out/share/applications/gpgui.desktop \ --replace-fail /usr/bin/gpclient $out/bin/gpclient substituteInPlace $out/share/polkit-1/actions/com.yuezk.gpgui.policy \ --replace-fail /usr/bin/gpservice $out/bin/gpservice \ --replace-fail /usr/libexec/gpclient/gp-vpnc-script-installer $out/bin/gp-vpnc-script-installer if [ -f $out/libexec/gpclient/gp-hip-script-installer ]; then substituteInPlace $out/share/polkit-1/actions/com.yuezk.gpgui.policy \ --replace-fail /usr/libexec/gpclient/gp-hip-script-installer $out/libexec/gpclient/gp-hip-script-installer fi if [ -f $out/lib/NetworkManager/dispatcher.d/pre-down.d/gpclient.down ]; then substituteInPlace $out/lib/NetworkManager/dispatcher.d/pre-down.d/gpclient.down \ --replace-fail /usr/bin/gpclient $out/bin/gpclient fi ''; rewriteHostInstallPaths = '' substituteInPlace $out/share/applications/gpgui.desktop \ --replace-fail /usr/bin/gpclient $out/bin/gpclient substituteInPlace $out/share/polkit-1/actions/com.yuezk.gpgui.policy \ --replace-fail /usr/bin/gpservice $out/bin/gpservice for installer in gp-vpnc-script-installer gp-hip-script-installer; do if [ -x "${prebuiltFiles}/libexec/gpclient/$installer" ]; then substituteInPlace $out/share/polkit-1/actions/com.yuezk.gpgui.policy \ --replace-fail "/usr/libexec/gpclient/$installer" "${prebuiltFiles}/libexec/gpclient/$installer" fi done if [ -f $out/lib/NetworkManager/dispatcher.d/pre-down.d/gpclient.down ]; then substituteInPlace $out/lib/NetworkManager/dispatcher.d/pre-down.d/gpclient.down \ --replace-fail /usr/bin/gpclient $out/bin/gpclient fi ''; installNixosPolkitRule = '' chmod u+w $out/share $out/share/polkit-1 2>/dev/null || true install -d $out/share/polkit-1/rules.d cat > $out/share/polkit-1/rules.d/49-gpgui.rules < $out/bin/gpclient <<'EOF' #!${pkgs.runtimeShell} set -eu export GP_SERVICE_BINARY='@gpservice_public@' export GP_AUTH_BINARY='@gpauth_public@' export GP_GUI_BINARY='${hostGuiLauncher}' exec '${prebuiltCommands.gpclient}/bin/gpclient' "$@" EOF substituteInPlace $out/bin/gpclient \ --replace-fail '@gpservice_public@' "$out/bin/gpservice" \ --replace-fail '@gpauth_public@' "$out/bin/gpauth" chmod +x $out/bin/gpclient cat > $out/bin/gpservice <<'EOF' #!${pkgs.runtimeShell} set -eu exec '@gpservice_fhs@' "$@" EOF substituteInPlace $out/bin/gpservice \ --replace-fail '@gpservice_fhs@' '${prebuiltCommands.gpservice}/bin/gpservice' chmod +x $out/bin/gpservice ln -s ${prebuiltCommands.gpauth}/bin/gpauth $out/bin/gpauth ln -s ${prebuiltCommands.gpgui}/bin/gpgui $out/bin/gpgui ln -s ${prebuiltCommands."gpgui-helper"}/bin/gpgui-helper $out/bin/gpgui-helper cp -r ${prebuiltFiles}/libexec $out/libexec cp -r ${prebuiltFiles}/share $out/share if [ -d ${prebuiltFiles}/lib ]; then cp -r ${prebuiltFiles}/lib $out/lib fi ${rewriteHostInstallPaths} ${installNixosPolkitRule} runHook postInstall ''; }; in { inherit fromSource prebuilt; }; nixosModule = { config, lib, pkgs, ... }: let cfg = config.programs.globalprotect-openconnect; in { options.programs.globalprotect-openconnect = { enable = lib.mkEnableOption "GlobalProtect-openconnect"; package = lib.mkOption { type = lib.types.package; # Use the NixOS module's package set so WebKit and graphics # dependencies match the rest of the system. default = (mkPackageSet pkgs).prebuilt; description = "GlobalProtect-openconnect package to install."; }; }; config = lib.mkIf cfg.enable { environment.systemPackages = [ cfg.package ]; services.ayatana-indicators.enable = lib.mkDefault true; }; }; in (flake-utils.lib.eachDefaultSystem ( system: let pkgs = (import nixpkgs) { inherit system; overlays = [ (import rust-overlay) ]; }; inherit (pkgs) lib; inherit (mkPackageSet pkgs) fromSource prebuilt; in { # For `nix build` packages = { fromSource = fromSource; } // lib.optionalAttrs pkgs.stdenv.isLinux { default = prebuilt; prebuilt = prebuilt; } // lib.optionalAttrs (!pkgs.stdenv.isLinux) { default = fromSource; }; apps.default = { type = "app"; program = "${self.packages.${system}.default}/bin/gpclient"; }; # For `nix develop`: not fully set up yet devShell = pkgs.mkShell { nativeBuildInputs = with pkgs; [ rustc cargo ]; }; } )) // { nixosModules.default = nixosModule; }; }