Skip to content

Commit

Permalink
Rework (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedorich-n authored Nov 7, 2024
1 parent 8fadc15 commit 471f176
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 249 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ Setup service:
user = "playit";
group = "playit";
secretPath = config.age.secrets.playit-secret.path;
runOverride = {
"890e3610-26cd-4e2b-b161-7cf0e4f69148".port = 8080;
"177485db-47aa-4fa9-9ccf-411ab761b9f0" = { ip = 192.168.1.1; port = 9000; };
};
};
}
```
Expand All @@ -58,21 +54,13 @@ Setup service:
Run

```Bash
nix run github:pedorich-n/playit-nixos-module#playit-cli -- claim generate
```

This will output a code, use this code in next command

```Bash
nix run github:pedorich-n/playit-nixos-module#playit-cli -- claim exchange <code>
nix run github:pedorich-n/playit-nixos-module#playit-cli -- start
```

Follow the link and approve the agent on the website. After that `plait-cli` will output a secret to the console.
Use this secret to create a TOML file like
The program will prompt the link to the website to claim the agent. Follow the instructions on the website.

```TOML
secret_key = "<secret>"
```
After the agent is claimed it will start running and serving the tunnels. You can exit the program at this point.
The TOML file containing the secret for newly claimed agent should be at `~/.config/playit_gg/playit.toml`. This file needs to be passed as `secretPath`.

It is recommended to use secret manager like [agenix](https://github.com/ryantm/agenix) or [sops](https://github.com/Mic92/sops-nix) to avoid having exposed secret in `/nix/store`

Expand Down
1 change: 1 addition & 0 deletions dev-extra-config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
37 changes: 0 additions & 37 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 10 additions & 30 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
systems.url = "github:nix-systems/default";
flake-parts.url = "github:hercules-ci/flake-parts";

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};

crane = {
url = "github:ipetkov/crane";
};

playit-agent-source = {
url = "github:playit-cloud/playit-agent";
flake = false;
Expand All @@ -30,27 +19,18 @@
outputs = inputs@{ flake-parts, ... }: flake-parts.lib.mkFlake { inherit inputs; } ({ moduleWithSystem, ... }: {
systems = import inputs.systems;

perSystem = { config, pkgs, system, ... }:
let
craneLib = inputs.crane.mkLib pkgs;
in
{
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ inputs.rust-overlay.overlays.default ];
};

packages = {
playit-cli = pkgs.callPackage ./nix/package.nix { inherit (inputs) playit-agent-source; inherit craneLib; };
default = config.packages.playit-cli;
docs = pkgs.callPackage ./nix/docs.nix { };
# mock = pkgs.callPackage ./test/mock-playit-cli { };
};
perSystem = { config, pkgs, ... }: {
packages = {
playit-cli = pkgs.callPackage ./nix/package.nix { inherit (inputs) playit-agent-source; };
default = config.packages.playit-cli;
docs = pkgs.callPackage ./nix/docs.nix { };
# mock = pkgs.callPackage ./test/mock-playit-cli.nix { };
};

checks = {
test-services-playit = pkgs.callPackage ./test/test-services-playit.nix { };
};
checks = {
test-services-playit = import ./test/test-services-playit.nix { inherit pkgs; };
};
};

flake = {
nixosModules.default = moduleWithSystem (perSystem@{ config }: { ... }: {
Expand Down
1 change: 0 additions & 1 deletion justfile

This file was deleted.

3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "dev/justfile.default"

extra_dev_config := justfile_directory() / 'dev-extra-config.nix'
68 changes: 14 additions & 54 deletions nix/nixos-module.nix
Original file line number Diff line number Diff line change
@@ -1,88 +1,48 @@
{ config, lib, ... }:
with lib;
let
cfg = config.services.playit;

localMappingType = with types; submodule {
options = {
ip = mkOption {
type = nullOr str;
description = "Local IP to route traffic to";
default = null;
};
port = mkOption {
type = nullOr port;
description = "Local port to route traffic to";
default = null;
};
};
};

maybeRunOverride =
let
ipPortString = { ip, port }:
if (ip == null && port == null) then throw "IP and Port can't both be empty!"
else concatStringsSep ":" (filter (x: x != null && x != "") [ ip (toString port) ]);

maybeOverridesList = lists.optionals (cfg.runOverride != { }) (attrsets.foldlAttrs
(acc: tunnelUUID: localMapping: acc ++ [ "${tunnelUUID}=${ipPortString localMapping}" ]) [ ]
cfg.runOverride);
in
strings.optionalString (maybeOverridesList != [ ]) ''run ${concatStringsSep "," maybeOverridesList}'';
in
{
###### interface
options = {
services.playit = {
enable = mkEnableOption "Playit Service";

package = mkOption {
type = types.package;
description = "Playit binary to run";
};
enable = lib.mkEnableOption "Playit Service";

runOverride = mkOption {
type = with types; attrsOf localMappingType;
description = "Attrset of local overrides. Name should be tunnel's UUID.";
default = { };
example = literalExpression ''
runOverride = {
"890e3610-26cd-4e2b-b161-7cf0e4f69148".port = 8080;
"177485db-47aa-4fa9-9ccf-411ab761b9f0" = { ip = 192.168.1.1; port = 9000; };
};
'';
package = lib.mkOption {
type = lib.types.package;
description = "playit binary to run";
};

secretPath = mkOption {
type = types.path;
secretPath = lib.mkOption {
type = lib.types.path;
description = "Path to TOML file containing secret";
};

user = mkOption {
type = types.str;
user = lib.mkOption {
type = lib.types.str;
default = "playit";
description = "User account under which Playit runs.";
};

group = mkOption {
type = types.str;
group = lib.mkOption {
type = lib.types.str;
default = "playit";
description = "Group under which Playit runs.";
};
};
};

###### implementation
config = mkIf cfg.enable {
users.users = optionalAttrs (cfg.user == "playit") {
config = lib.mkIf cfg.enable {
users.users = lib.optionalAttrs (cfg.user == "playit") {
playit = {
isSystemUser = true;
group = "playit";
description = "Playit daemon user";
};
};

users.groups = optionalAttrs (cfg.group == "playit") {
users.groups = lib.optionalAttrs (cfg.group == "playit") {
playit = { };
};

Expand All @@ -94,7 +54,7 @@ in
after = [ "network.target" "systemd-resolved.service" ];

script = ''
${getExe cfg.package} --secret_wait --secret_path ${cfg.secretPath} ${maybeRunOverride}
${lib.getExe cfg.package} --stdout --secret_wait --secret_path ${cfg.secretPath} start
'';

serviceConfig = {
Expand Down
19 changes: 13 additions & 6 deletions nix/package.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{ pkgs, playit-agent-source, craneLib, ... }:
{ playit-agent-source, rustPlatform, lib, ... }:
let
toolchain = pkgs.rust-bin.stable.latest.default;
craneLibWithOverride = craneLib.overrideToolchain toolchain;
src = lib.cleanSource playit-agent-source;
cargoLock = "${src}/Cargo.lock";
cargoToml = lib.importTOML "${src}/Cargo.toml";
in
craneLibWithOverride.buildPackage {
pname = "playit-cli";
rustPlatform.buildRustPackage {
pname = "playit-agent";
meta.mainProgram = "playit-cli";
src = craneLib.cleanCargoSource playit-agent-source;
inherit (cargoToml.workspace.package) version;

inherit src;
cargoLock = {
lockFile = cargoLock;
};

strictDeps = true;
# Requires internet access
doCheck = false;
}
11 changes: 11 additions & 0 deletions test/mock-playit-cli.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{ runCommandLocal, simple-http-server, lib, ... }:
runCommandLocal "mock-playit-cli"
{
meta.mainProgram = "playit-cli";
} ''
mkdir -p $out/bin
bin=$out/bin/playit-cli
echo "${lib.getExe simple-http-server} --port 9213" >> $bin
chmod +x $bin
''
15 changes: 0 additions & 15 deletions test/mock-playit-cli/default.nix

This file was deleted.

32 changes: 0 additions & 32 deletions test/mock-playit-cli/main.py

This file was deleted.

10 changes: 0 additions & 10 deletions test/mock-playit-cli/pyproject.toml

This file was deleted.

8 changes: 0 additions & 8 deletions test/snapshots/multiple-overrides.json

This file was deleted.

4 changes: 0 additions & 4 deletions test/snapshots/no-overrides.json

This file was deleted.

Loading

0 comments on commit 471f176

Please sign in to comment.