From a4e1743691ac4882fe691c06615cecd3de1b2d26 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Tue, 4 Nov 2025 23:12:03 +0800 Subject: [PATCH] feat: add NixOS support with flake configuration and package setup --- .github/workflows/build.yaml | 1 + .gitignore | 3 + README.md | 39 ++++++++ flake.lock | 128 ++++++++++++++++++++------ flake.nix | 168 +++++++++++++++++++++++++---------- 5 files changed, 266 insertions(+), 73 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 735e527..4cad8c0 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,5 +1,6 @@ name: Build on: + workflow_dispatch: push: paths-ignore: - LICENSE diff --git a/.gitignore b/.gitignore index 2caa3e7..0ec367a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ SNAPSHOT # Tauri generated files gen + +# Nix files +result diff --git a/README.md b/README.md index ddd6171..baf6a9b 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,45 @@ sudo emerge -r guru sync sudo emerge -av net-vpn/GlobalProtect-openconnect ``` +### NixOS + +This repo includes a flake for NixOS. You can add the following to your `flake.nix`: + +```nix +{ + inputs = { + # ... other inputs + globalprotect-openconnect.url = "github:yuezk/GlobalProtect-openconnect"; + }; + + outputs = { nixpkgs, ... }@inputs: { + nixosConfigurations. = nixpkgs.lib.nixosSystem { + specialArgs = { inherit inputs; }; + modules = [ + ./configuration.nix + ]; + }; + }; +} +``` + +Then add `globalprotect-openconnect` to your `environment.systemPackages` in `configuration.nix`: + +```nix +{ config, pkgs, inputs, ... }: + +{ + # ... other configurations + environment.systemPackages = with pkgs; [ + # ... other packages + ] ++ [ + inputs.globalprotect-openconnect.packages.${pkgs.system}.default + ]; +} +``` + +Finally, run `sudo nixos-rebuild switch` to apply the changes. + ### Other distributions - Install `openconnect >= 8.20`, `webkit2gtk`, `libsecret`, `libayatana-appindicator` or `libappindicator-gtk3`. diff --git a/flake.lock b/flake.lock index 660c45e..a3bea3b 100644 --- a/flake.lock +++ b/flake.lock @@ -1,15 +1,56 @@ { "nodes": { + "fenix": { + "inputs": { + "nixpkgs": [ + "naersk", + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1752475459, + "narHash": "sha256-z6QEu4ZFuHiqdOPbYss4/Q8B0BFhacR8ts6jO/F/aOU=", + "owner": "nix-community", + "repo": "fenix", + "rev": "bf0d6f70f4c9a9cf8845f992105652173f4b617f", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "naersk": { "inputs": { + "fenix": "fenix", "nixpkgs": "nixpkgs" }, "locked": { - "lastModified": 1718727675, - "narHash": "sha256-uFsCwWYI2pUpt0awahSBorDUrUfBhaAiyz+BPTS2MHk=", + "lastModified": 1752689277, + "narHash": "sha256-uldUBFkZe/E7qbvxa3mH1ItrWZyT6w1dBKJQF/3ZSsc=", "owner": "nix-community", "repo": "naersk", - "rev": "941ce6dc38762a7cfb90b5add223d584feed299b", + "rev": "0e72363d0938b0208d6c646d10649164c43f4d64", "type": "github" }, "original": { @@ -20,39 +61,43 @@ }, "nixpkgs": { "locked": { - "lastModified": 0, - "narHash": "sha256-US1tAChvPxT52RV8GksWZS415tTS7PV42KTc2PNDBmc=", - "path": "/nix/store/aa0zsahvyqnvzkicsh29jirm9hwv95pg-source", - "type": "path" - }, - "original": { - "id": "nixpkgs", - "type": "indirect" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1720244366, - "narHash": "sha256-WrDV0FPMVd2Sq9hkR5LNHudS3OSMmUrs90JUTN+MXpA=", + "lastModified": 1752077645, + "narHash": "sha256-HM791ZQtXV93xtCY+ZxG1REzhQenSQO020cu6rHtAPk=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "49ee0e94463abada1de470c9c07bfc12b36dcf40", + "rev": "be9e214982e20b8310878ac2baa063a961c1bdf6", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixos-24.05", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1761880412, + "narHash": "sha256-QoJjGd4NstnyOG4mm4KXF+weBzA2AH/7gn1Pmpfcb0A=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a7fc11be66bdfb5cdde611ee5ce381c183da8386", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", "repo": "nixpkgs", "type": "github" } }, "nixpkgs_3": { "locked": { - "lastModified": 1718428119, - "narHash": "sha256-WdWDpNaq6u1IPtxtYHHWpl5BmabtpmLnMAx0RdJ/vo8=", + "lastModified": 1744536153, + "narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e6cea36f83499eb4e9cd184c8a8e823296b50ad5", + "rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11", "type": "github" }, "original": { @@ -64,21 +109,39 @@ }, "root": { "inputs": { + "flake-utils": "flake-utils", "naersk": "naersk", "nixpkgs": "nixpkgs_2", "rust-overlay": "rust-overlay" } }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1752428706, + "narHash": "sha256-EJcdxw3aXfP8Ex1Nm3s0awyH9egQvB2Gu+QEnJn2Sfg=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "591e3b7624be97e4443ea7b5542c191311aa141d", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, "rust-overlay": { "inputs": { "nixpkgs": "nixpkgs_3" }, "locked": { - "lastModified": 1720318855, - "narHash": "sha256-w3CCVK9LJ5aznXGkO1IyAlbvMNJfyA+dBF7Z1Zwx1LA=", + "lastModified": 1761878277, + "narHash": "sha256-6fCtyVdTzoQejwoextAu7dCLoy5fyD3WVh+Qm7t2Nhg=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "3eed08a074cd2000884a69d448d70da2843f7103", + "rev": "6604534e44090c917db714faa58d47861657690c", "type": "github" }, "original": { @@ -86,6 +149,21 @@ "repo": "rust-overlay", "type": "github" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index a16b237..925474d 100644 --- a/flake.nix +++ b/flake.nix @@ -1,59 +1,131 @@ -# This flake was initially generated by fh, the CLI for FlakeHub (version 0.1.12) { - # Flake inputs inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; + flake-utils.url = "github:numtide/flake-utils"; naersk.url = "github:nix-community/naersk"; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; rust-overlay.url = "github:oxalica/rust-overlay"; }; - # Flake outputs that other flakes can use - outputs = { - self, - nixpkgs, - naersk, - rust-overlay, - }: let - supportedSystems = ["x86_64-linux" "aarch64-linux"]; - forEachSupportedSystem = f: - nixpkgs.lib.genAttrs supportedSystems (system: - f rec { - pkgs = import nixpkgs { - inherit system; - overlays = [rust-overlay.overlays.default]; - }; - naersk' = pkgs.callPackage naersk {}; - }); - in { - packages = forEachSupportedSystem ({ - pkgs, - naersk', - }: { - default = naersk'.buildPackage { - src = ./.; - nativeBuildInputs = with pkgs; [ - perl - jq - openconnect - libsoup - webkitgtk - pkg-config - ]; + outputs = + { + self, + flake-utils, + naersk, + nixpkgs, + rust-overlay, + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = (import nixpkgs) { + inherit system; + overlays = [ (import rust-overlay) ]; + }; - overrideMain = {...}: { - postPatch = '' - substituteInPlace crates/common/src/vpn_utils.rs \ - --replace-fail /etc/vpnc/vpnc-script ${pkgs.vpnc-scripts}/bin/vpnc-script - substituteInPlace crates/gpapi/src/lib.rs \ - --replace-fail /usr/bin/gpclient $out/bin/gpclient \ - --replace-fail /usr/bin/gpservice $out/bin/gpservice \ - --replace-fail /usr/bin/gpgui-helper $out/bin/gpgui-helper \ - --replace-fail /usr/bin/gpgui $out/bin/gpgui \ - --replace-fail /usr/bin/gpauth $out/bin/gpauth + cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); + pname = "globalprotect-openconnect"; + version = cargoToml.workspace.package.version; + + toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + + naersk' = pkgs.callPackage naersk { + cargo = toolchain; + rustc = toolchain; + }; + + cpu = pkgs.stdenv.hostPlatform.parsed.cpu.name; + + gpgui = pkgs.fetchzip { + url = "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/v${version}/gpgui_${cpu}.bin.tar.xz"; + hash = "sha256-dy8McFjcOAvRGEc8Al9PA7LKfxJNZycSEppE4FmqT1Q="; + }; + in + { + # For `nix build` + packages.default = naersk'.buildPackage { + inherit pname version; + + # Must be set to true to avoid issues with the Tauri build process + singleStep = true; + + src = ./.; + + buildInputs = + with pkgs; + [ + openconnect + ] + ++ lib.optionals stdenv.isLinux [ + glib + gtk3 + libsoup_3 + webkitgtk_4_1 + ]; + + nativeBuildInputs = + with pkgs; + [ ] + ++ lib.optionals stdenv.isLinux [ + autoPatchelfHook + ]; + + runtimeDependencies = + with pkgs; + [ ] + ++ lib.optionals stdenv.isLinux [ + libappindicator-gtk3 + ]; + + overrideMain = + { ... }: + { + postPatch = '' + substituteInPlace crates/openconnect/src/vpn_utils.rs \ + --replace-fail /etc/vpnc/vpnc-script ${pkgs.vpnc-scripts}/bin/vpnc-script \ + --replace-fail /usr/lib/openconnect/hipreport.sh ${pkgs.openconnect}/libexec/openconnect/hipreport.sh + + substituteInPlace crates/common/src/constants.rs \ + --replace-fail /usr/bin/gpclient $out/bin/gpclient \ + --replace-fail /usr/bin/gpservice $out/bin/gpservice \ + --replace-fail /usr/bin/gpgui-helper $out/bin/gpgui-helper \ + --replace-fail /usr/bin/gpgui $out/bin/gpgui \ + --replace-fail /usr/bin/gpauth $out/bin/gpauth + ''; + }; + + postInstall = '' + # Copy the prebuilt gpgui binary to the output bin directory + cp ${gpgui}/gpgui $out/bin/gpgui + chmod +x $out/bin/gpgui + + cp -r packaging/files/usr/share $out/share + cp -r packaging/files/usr/lib $out/lib + + # Change the `/usr/bin/gpclient` path in the desktop file + substituteInPlace $out/share/applications/gpgui.desktop \ + --replace-fail /usr/bin/gpclient $out/bin/gpclient + + substituteInPlace $out/lib/NetworkManager/dispatcher.d/pre-down.d/gpclient.down \ + --replace-fail /usr/bin/gpclient $out/bin/gpclient + + # Change the `/usr/bin/gpservice` path in the polkit policy file + substituteInPlace $out/share/polkit-1/actions/com.yuezk.gpgui.policy \ + --replace-fail /usr/bin/gpservice $out/bin/gpservice ''; }; - }; - }); - }; + 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 + ]; + }; + } + ); }