{ 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, }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = (import nixpkgs) { inherit system; overlays = [ (import rust-overlay) ]; }; inherit (pkgs) lib; cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); pname = "globalprotect-openconnect"; version = cargoToml.workspace.package.version; releaseTag = "snapshot"; toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; naersk' = pkgs.callPackage naersk { cargo = toolchain; rustc = toolchain; }; src = pkgs.fetchzip { url = "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/${releaseTag}/globalprotect-openconnect-${version}.tar.gz"; hash = "sha256-jbv5OX7XeLZkY6y+eGbKEg77TKJDpmvzzwVWTjqKFJQ="; }; cpu = pkgs.stdenv.hostPlatform.parsed.cpu.name; gpguiHashes = { x86_64 = "sha256-QPPQoR3QpA7evloCrjDb+0XsF0d1ONw5D2mvoaECGiY="; aarch64 = "sha256-tDmliqiNZZ2pkt2V51OKDILt2IjdIasduvPZKxLOf0U="; }; gpgui = pkgs.fetchzip { url = "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/${releaseTag}/gpgui_${cpu}.bin.tar.xz"; hash = gpguiHashes.${cpu}; }; binaryHashes = { x86_64 = "sha256-lR64CR4yG0pV5tMJaYz9+EVYsIPnYrZdXsZNCLmhNwE="; aarch64 = "sha256-nkXdTNkDevS/Q5bbDWw50y8ciYI5TJst2M9TGRl/SKQ="; }; binaryPackage = pkgs.fetchzip { url = "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/${releaseTag}/globalprotect-openconnect_${version}_${cpu}.bin.tar.xz"; hash = binaryHashes.${cpu}; }; linuxRuntimeDependencies = with pkgs; [ glib-networking libayatana-appindicator xdg-utils ]; 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 = '' substituteInPlace $out/libexec/gpclient/hipreport.sh \ --replace-fail /usr/bin/gpclient $out/bin/gpclient '' + 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/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 \ --replace-fail /usr/libexec/gpclient/gp-vpnc-script-installer ${prebuiltFiles}/libexec/gpclient/gp-vpnc-script-installer 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 gpclient_fhs='${prebuiltCommands.gpclient}/bin/gpclient' gpservice_public='@gpservice_public@' gpauth_public='@gpauth_public@' if [ "''${1:-}" = "launch-gui" ]; then shift minimized= callback= for arg in "$@"; do case "$arg" in --minimized) minimized=--minimized ;; globalprotectcallback:*) if [ -n "$callback" ]; then echo "Multiple authentication callback URLs were provided" >&2 exit 2 fi callback=$arg ;; *) echo "Unsupported launch-gui argument: $arg" >&2 exit 2 ;; esac done if [ -n "$callback" ]; then if [ -n "$minimized" ]; then echo "An authentication callback cannot be combined with --minimized" >&2 exit 2 fi exec "$gpauth_public" auth-callback "$callback" fi if [ -n "''${XDG_DATA_HOME:-}" ]; then data_home=$XDG_DATA_HOME elif [ -n "''${HOME:-}" ]; then data_home=$HOME/.local/share else data_home=/tmp fi log_dir="$data_home/gpclient" mkdir -p "$log_dir" log_file="$log_dir/gpclient.log" env_file=$(mktemp) env > "$env_file" printf 'GP_LOG_FILE=%s\n' "$log_file" >> "$env_file" pkexec_bin=/run/wrappers/bin/pkexec if [ ! -x "$pkexec_bin" ]; then pkexec_bin=pkexec fi set +e if [ -n "$minimized" ]; then "$pkexec_bin" --user root "$gpservice_public" --minimized --env-file "$env_file" 2>"$log_file" else "$pkexec_bin" --user root "$gpservice_public" --env-file "$env_file" 2>"$log_file" fi status=$? set -e rm -f "$env_file" exit "$status" fi exec "$gpclient_fhs" "$@" 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 { # 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 ]; }; } ); }