Skip to content
This repository has been archived by the owner on Jan 25, 2025. It is now read-only.

Commit

Permalink
Added nix module for zia-server
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelCoding committed Dec 9, 2023
1 parent 8f9156b commit c500995
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 34 deletions.
12 changes: 12 additions & 0 deletions derivation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs, fenix, system, cargoToml, ... }:
let
manifest = (pkgs.lib.importTOML cargoToml).package;
in
pkgs.rustPlatform.buildRustPackage {
pname = manifest.name;
version = manifest.version;
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
cargoBuildFlags = "-p ${manifest.name}";
nativeBuildInputs = [ fenix.packages.${system}.stable.toolchain ];
}
18 changes: 9 additions & 9 deletions flake.lock

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

51 changes: 26 additions & 25 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,33 @@
};

outputs = { self, nixpkgs, flake-utils, fenix }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit system;
};
in
rec {
packages = {
zia-client = let manifest = (pkgs.lib.importTOML ./zia-client/Cargo.toml).package; in pkgs.rustPlatform.buildRustPackage {
pname = manifest.name;
version = manifest.version;
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
cargoBuildFlags = "-p ${manifest.name}";
nativeBuildInputs = [ fenix.packages.${system}.stable.toolchain ];
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = (import nixpkgs) {
inherit system;
};
in
{
packages = {
zia-client = pkgs.callPackage ./derivation.nix {
inherit fenix;
cargoToml = ./zia-client/Cargo.toml;
};

zia-server = let manifest = (pkgs.lib.importTOML ./zia-server/Cargo.toml).package; in pkgs.rustPlatform.buildRustPackage {
pname = manifest.name;
version = manifest.version;
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
cargoBuildFlags = "-p ${manifest.name}";
nativeBuildInputs = [ fenix.packages.${system}.stable.toolchain ];
zia-server = pkgs.callPackage ./derivation.nix {
inherit fenix;
cargoToml = ./zia-server/Cargo.toml;
};
};
};
}
);
}
) // {
overlays.default = final: prev: {
inherit (self.packages.${final.system}) zia-server zia-client;
};

nixosModules = {
zia-server = import ./nixos-modules/zia-server.nix;
};
};
}
88 changes: 88 additions & 0 deletions nixos-modules/zia-server.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{ config, pkgs, lib, ... }:

let
cfg = config.services.zia-server;
ziaServerName = name: "zia-server" + "-" + name;
enabledServers = lib.filterAttrs (name: conf: conf.enable) config.services.zia-server.servers;
in
{
options = {
services.zia-server = {
package = lib.mkOption {
type = lib.types.package;
default = pkgs.zia-server;
defaultText = lib.literalExpression "pkgs.zia-server";
description = lib.mdDoc "Which Zia Server derivation to use.";
};

servers = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule ({ config, name, ... }: {
options = {
enable = lib.mkEnableOption (lib.mdDoc "Zia Server.");
listen-addr = lib.mkOption {
type = lib.types.str;
description = lib.mkDoc "The socket address zia should be listening on.";
default = null;
};
upstream = lib.mkOption {
type = lib.types.str;
description = lib.mkDoc "The socket address of the udp upstream zia should redirect all traffic to.";
default = null;
};
mode = lib.mkOption {
type = lib.types.enum [ "ws" ];
description = lib.mkDoc "The mode zia sould be listening with.";
default = "ws";
};
user = lib.mkOption {
type = lib.types.str;
default = lib.ziaServerName name;
defaultText = lib.literalExpression ''
"zia-server-''${name}"
'';
description = lib.mdDoc "The username and groupname for zia-server.";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc "Whether to open ports in the firewall for the server.";
};
};
}));
};
};
};

config = lib.mkIf (enabledServers != { }) {
environment.systemPackages = [ cfg.package ];

users.users = lib.mapAttrs'
(name: conf: lib.nameValuePair (ziaServerName name) {
description = "System user for the zia-server instance ${name}";
isSystemUser = true;
group = ziaServerName name;
})
enabledServers;

users.groups = lib.mapAttrs'
(name: conf: lib.nameValuePair (ziaServerName name) { })
enabledServers;

systemd.services = lib.mapAttrs'
(name: conf: lib.nameValuePair (ziaServerName name) {
description = "Zia Server - ${ziaServerName name}";

wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];

serviceConfig = {
ExecStart = "${cfg.package}/bin/zia-server";
Type = "notify";
# User and group
User = conf.user;
Group = conf.user;
};
})
enabledServers;
};
}

0 comments on commit c500995

Please sign in to comment.