flake reorg

This commit is contained in:
Robin Appelman 2024-01-06 23:35:55 +01:00
commit bc6b1b1c8f
4 changed files with 177 additions and 125 deletions

160
flake.nix
View file

@ -21,7 +21,10 @@
cross-naersk, cross-naersk,
}: }:
utils.lib.eachDefaultSystem (system: let utils.lib.eachDefaultSystem (system: let
overlays = [ (import rust-overlay) ]; overlays = [
(import rust-overlay)
(import ./overlay.nix)
];
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system overlays; inherit system overlays;
}; };
@ -44,34 +47,31 @@
cross-naersk' = pkgs.callPackage cross-naersk {inherit naersk;}; cross-naersk' = pkgs.callPackage cross-naersk {inherit naersk;};
addUdev = ''
mkdir -p $out/lib/udev/rules.d/
echo 'SUBSYSTEM=="powercap", ACTION=="add", RUN+="${pkgs.coreutils-full}/bin/chgrp -R powermonitoring /sys%p", RUN+="${pkgs.coreutils-full}/bin/chmod -R g=u /sys%p"' >> $out/lib/udev/rules.d/51-palantir.rules
echo 'SUBSYSTEM=="powercap", ACTION=="change", ENV{TRIGGER}!="none", RUN+="${pkgs.coreutils-full}/bin/chgrp -R powermonitoring /sys%p", RUN+="${pkgs.coreutils-full}/bin/chmod -R g=u /sys%p"' >> $out/lib/udev/rules.d/51-palantir.rules
'';
src = lib.sources.sourceByRegex (lib.cleanSource ./.) ["Cargo.*" "(src|benches)(/.*)?"]; src = lib.sources.sourceByRegex (lib.cleanSource ./.) ["Cargo.*" "(src|benches)(/.*)?"];
nearskOpt = { nearskOpt = {
pname = "palantir"; pname = "palantir";
root = src; root = src;
postInstall = addUdev; postInstall = pkgs.palantir.postInstall;
}; };
buildTarget = target: (cross-naersk'.buildPackage target) nearskOpt; buildTarget = target: (cross-naersk'.buildPackage target) nearskOpt;
hostNaersk = cross-naersk'.hostNaersk; hostNaersk = cross-naersk'.hostNaersk;
in rec { in rec {
# `nix build` packages =
packages = nixpkgs.lib.attrsets.genAttrs targets buildTarget // rec { nixpkgs.lib.attrsets.genAttrs targets buildTarget
palantir = packages.${hostTarget}; // rec {
check = hostNaersk.buildPackage (nearskOpt // { palantir = pkgs.palantir;
mode = "check"; check = hostNaersk.buildPackage (nearskOpt
}); // {
clippy = hostNaersk.buildPackage (nearskOpt // { mode = "check";
mode = "clippy"; });
}); clippy = hostNaersk.buildPackage (nearskOpt
default = palantir; // {
}; mode = "clippy";
});
default = palantir;
};
apps.palantir = utils.lib.mkApp { apps.palantir = utils.lib.mkApp {
drv = packages.palantir; drv = packages.palantir;
@ -80,11 +80,13 @@
inherit targets; inherit targets;
releaseMatrix = { releaseMatrix = {
include = builtins.map (target: { include =
inherit target; builtins.map (target: {
artifact_name = artifactForTarget target; inherit target;
asset_name = assetNameForTarget target; artifact_name = artifactForTarget target;
}) releaseTargets; asset_name = assetNameForTarget target;
})
releaseTargets;
}; };
# `nix develop` # `nix develop`
@ -95,110 +97,18 @@
}; };
}) })
// { // {
nixosModule = { overlays = import ./overlay.nix;
modules.default = {
pkgs,
config, config,
lib, lib,
pkgs,
... ...
}: }: {
with lib; let imports = [./module.nix];
cfg = config.palantir.services.palantir; config = lib.mkIf config.services.palantir.enable {
in { nixpkgs.overlays = [self.overlays.default];
options.palantir.services.palantir = { services.palantir.package = lib.mkDefault pkgs.palantir;
enable = mkEnableOption "Enables the palantir service";
port = mkOption rec {
type = types.int;
default = 5665;
example = default;
description = "The port to listen on";
};
zfs = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "enable zfs integration";
};
docker = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "enable docker integration";
};
mdns = mkOption rec {
type = types.bool;
default = true;
example = true;
description = "enable mdns discovery";
};
openPort = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "open port";
};
openMDNSPort = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "open mdns port";
};
};
config = mkIf cfg.enable {
networking.firewall.allowedTCPPorts = lib.optional cfg.openPort cfg.port;
networking.firewall.allowedUDPPorts = lib.optional cfg.openMDNSPort 5353;
users.groups.powermonitoring = {};
services.udev.packages = [self.packages.${pkgs.system}.default];
systemd.services."palantir" = {
wantedBy = ["multi-user.target"];
path = lib.optional cfg.zfs pkgs.zfs;
environment = {
PORT = "${toString cfg.port}";
LD_LIBRARY_PATH = "/run/opengl-driver/lib/"; # needed for nvidia
} // (if (cfg.mdns == false) then {
DISABLE_MDNS = "true";
} else {});
serviceConfig = let
pkg = self.packages.${pkgs.system}.default;
in {
Restart = "on-failure";
ExecStart = "${pkg}/bin/palantir";
DynamicUser = true;
PrivateTmp = true;
PrivateUsers = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
ProtectClock = !cfg.zfs; # Enabling this breaks libzfs
CapabilityBoundingSet = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
SystemCallArchitectures = "native";
ProtectKernelModules = true;
RestrictNamespaces = true;
MemoryDenyWriteExecute = true;
ProtectHostname = true;
LockPersonality = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = ["AF_INET" "AF_INET6" "AF_NETLINK"] ++ lib.optional cfg.docker "AF_UNIX"; # netlink is required to make `getifaddrs` not err
RestrictRealtime = true;
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressAllow = ["localhost"] ++ lib.optional cfg.mdns "multicast";
UMask = "0077";
SupplementaryGroups = ["powermonitoring"] ++ lib.optional cfg.docker "docker";
};
};
};
}; };
};
}; };
} }

114
module.nix Normal file
View file

@ -0,0 +1,114 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.palantir.services.palantir;
in {
options.palantir.services.palantir = {
enable = mkEnableOption "Enables the palantir service";
port = mkOption rec {
type = types.int;
default = 5665;
example = default;
description = "The port to listen on";
};
zfs = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "enable zfs integration";
};
docker = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "enable docker integration";
};
mdns = mkOption rec {
type = types.bool;
default = true;
example = true;
description = "enable mdns discovery";
};
openPort = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "open port";
};
openMDNSPort = mkOption rec {
type = types.bool;
default = false;
example = true;
description = "open mdns port";
};
package = mkOption {
type = types.package;
description = "package to use";
};
};
config = mkIf cfg.enable {
networking.firewall.allowedTCPPorts = lib.optional cfg.openPort cfg.port;
networking.firewall.allowedUDPPorts = lib.optional cfg.openMDNSPort 5353;
users.groups.powermonitoring = {};
services.udev.packages = [cfg.package];
systemd.services."palantir" = {
wantedBy = ["multi-user.target"];
path = lib.optional cfg.zfs pkgs.zfs;
environment =
{
PORT = "${toString cfg.port}";
LD_LIBRARY_PATH = "/run/opengl-driver/lib/"; # needed for nvidia
}
// (
if (cfg.mdns == false)
then {
DISABLE_MDNS = "true";
}
else {}
);
serviceConfig = {
Restart = "on-failure";
ExecStart = "${cfg.package}/bin/palantir";
DynamicUser = true;
PrivateTmp = true;
PrivateUsers = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
ProtectClock = !cfg.zfs; # Enabling this breaks libzfs
CapabilityBoundingSet = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
SystemCallArchitectures = "native";
ProtectKernelModules = true;
RestrictNamespaces = true;
MemoryDenyWriteExecute = true;
ProtectHostname = true;
LockPersonality = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = ["AF_INET" "AF_INET6" "AF_NETLINK"] ++ lib.optional cfg.docker "AF_UNIX"; # netlink is required to make `getifaddrs` not err
RestrictRealtime = true;
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressAllow = ["localhost"] ++ lib.optional cfg.mdns "multicast";
UMask = "0077";
SupplementaryGroups = ["powermonitoring"] ++ lib.optional cfg.docker "docker";
};
};
};
}

3
overlay.nix Normal file
View file

@ -0,0 +1,3 @@
final: prev: {
palantir = final.callPackage ./package.nix {};
}

25
package.nix Normal file
View file

@ -0,0 +1,25 @@
{
stdenv,
rustPlatform,
coreutils,
lib,
}: let
inherit (lib.sources) sourceByRegex;
src = sourceByRegex ./. ["Cargo.*" "(src|benches)(/.*)?"];
in
rustPlatform.buildRustPackage rec {
name = "palantir";
version = "0.1.0";
inherit src;
cargoLock = {
lockFile = ./Cargo.lock;
};
postInstall = ''
mkdir -p $out/lib/udev/rules.d/
echo 'SUBSYSTEM=="powercap", ACTION=="add", RUN+="${coreutils}/bin/chgrp -R powermonitoring /sys%p", RUN+="${coreutils}/bin/chmod -R g=u /sys%p"' >> $out/lib/udev/rules.d/51-palantir.rules
echo 'SUBSYSTEM=="powercap", ACTION=="change", ENV{TRIGGER}!="none", RUN+="${coreutils}/bin/chgrp -R powermonitoring /sys%p", RUN+="${coreutils}/bin/chmod -R g=u /sys%p"' >> $out/lib/udev/rules.d/51-palantir.rules
'';
}