aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-10-14 04:03:21 +0000
committerFuwn <[email protected]>2024-10-14 04:03:21 +0000
commit246da04082bc08d4ee2ac01f353259d25d182915 (patch)
treed9472200b3c53d26ed73724d9061644ae1a0dec2
parentrefactor(flake): use system-specific nixpkgs everywhere (diff)
downloadpia.nix-246da04082bc08d4ee2ac01f353259d25d182915.tar.xz
pia.nix-246da04082bc08d4ee2ac01f353259d25d182915.zip
feat(flake): add a proper cli
-rw-r--r--README.md22
-rw-r--r--cli.nix54
-rw-r--r--flake.nix41
3 files changed, 88 insertions, 29 deletions
diff --git a/README.md b/README.md
index 923e98b..3977d71 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# 🔒 `pia.nix`
-> Private Internet Access VPN Configurations for NixOS
+> Private Internet Access VPN Configurations & CLI for NixOS
## Flake-based Installation
@@ -36,17 +36,23 @@ attribute set.
## Usage
```sh
+# List all available VPN regions
+pia list
+
+# List all available VPN regions with fuzzy search support
+pia search
+
# Activate VPN in a specific region
-pia-start japan
+pia start japan
-# Deactivate VPN
-pia-stop japan
+# Uses `pia search` to activate VPN in a specific region
+pia start
-# List all available VPN regions
-pia-list
+# Deactivate VPN in a specific region
+pia stop japan
-# List all available VPN regions with fuzzy search support
-pia-search
+# Uses `pia search` to deactivate VPN in a specific region
+pia stop
```
## Credits
diff --git a/cli.nix b/cli.nix
new file mode 100644
index 0000000..2fa39eb
--- /dev/null
+++ b/cli.nix
@@ -0,0 +1,54 @@
+{ lib, pkgs }:
+let
+ inherit (lib) getExe;
+in
+pkgs.writeShellScriptBin "pia" ''
+ ID=$(${pkgs.coreutils}/bin/id -u)
+
+ if [ "$ID" -ne 0 ]; then
+ if [ -x $(command -v doas) ]; then
+ exec doas "$0" "$@"
+ else
+ exec sudo "$0" "$@"
+ fi
+ fi
+
+ COMMAND="$1"
+ LOCATION="$2"
+
+ list() {
+ ${getExe pkgs.findutils} /etc/systemd/system/ -name 'openvpn-*.service' -exec ${pkgs.coreutils}/bin/basename {} .service \; |
+ sed 's/openvpn-//'
+ }
+
+ search() {
+ list | ${getExe pkgs.fzf}
+ }
+
+ search_location() {
+ if [ -z "$LOCATION" ]; then
+ LOCATION=$(search)
+ fi
+ }
+
+ case "$COMMAND" in
+ "start")
+ search_location
+ sudo systemctl start "openvpn-$LOCATION.service"
+ ;;
+ "stop")
+ search_location
+ sudo systemctl stop "openvpn-$LOCATION.service"
+ ;;
+ "list")
+ list
+ ;;
+ "search")
+ search
+ ;;
+ *)
+ ${pkgs.coreutils}/bin/echo "usage: $0 <start|stop|list|search>"
+ exit 1
+ ;;
+ esac
+''
diff --git a/flake.nix b/flake.nix
index b1f2940..c194ada 100644
--- a/flake.nix
+++ b/flake.nix
@@ -13,36 +13,35 @@
flake-utils.lib.eachDefaultSystem (
system:
let
+ inherit (pkgs) lib;
+
pkgs = import nixpkgs {
inherit system;
};
-
- lib = pkgs.lib;
in
{
- packages =
- let
- makeShellScript =
- name: action:
- pkgs.writeScriptBin name ''
- #!${pkgs.runtimeShell}
+ packages = {
+ pia = import ./cli.nix { inherit lib pkgs; };
+ default = self.packages.${system}.pia;
+ };
- if [ "$(id -u)" -ne 0 ]; then
- exec sudo "$0" "$@"
- fi
+ apps = {
+ default = self.apps.${system}.pia;
- ${action}
- '';
- in
- {
- pia-start = makeShellScript "pia-start" "sudo systemctl start openvpn-$1.service";
- pia-stop = makeShellScript "pia-stop" "sudo systemctl stop openvpn-$1.service";
- pia-list = makeShellScript "pia-list" "ls /etc/systemd/system/ | awk '/openvpn/ {gsub(/openvpn-|.service/, \"\"); print}'";
+ pia = {
+ type = "app";
+ program = "${self.packages.${system}.pia}/bin/pia";
- pia-search = makeShellScript "pia-search" "${
- lib.getExe self.packages.${system}.pia-list
- } | ${lib.getExe pkgs.fzf}";
+ meta = with pkgs.lib; {
+ description = "Private Internet Access VPN CLI for NixOS";
+ license = licenses.gpl3Only;
+ maintainers = [ maintainers.Fuwn ];
+ homepage = "https://github.com/Fuwn/pia.nix";
+ mainPackage = "pia";
+ platforms = platforms.linux;
+ };
};
+ };
nixosModules.default =
{ config, ... }: