From a2d09d2ff5731cc4487daa3073e8413b40e046ae Mon Sep 17 00:00:00 2001 From: magic_rb Date: Thu, 26 Dec 2024 14:55:06 +0100 Subject: [PATCH 1/3] Add `nglib.mkUserOption` and `nglib.mkGroupOption` Signed-off-by: magic_rb --- lib/default.nix | 10 ++++++++++ lib/options.nix | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 lib/options.nix diff --git a/lib/default.nix b/lib/default.nix index f3a6a25..6fd3b9e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -78,3 +78,13 @@ let }; in this +// { + inherit + (import ./options.nix { + inherit lib; + nglib = this; + }) + mkUserOption + mkGroupOption + ; +} diff --git a/lib/options.nix b/lib/options.nix new file mode 100644 index 0000000..6579075 --- /dev/null +++ b/lib/options.nix @@ -0,0 +1,18 @@ +{ lib, nglib, ... }: +{ + mkUserOption = + user: description: + lib.mkOption { + inherit description; + type = lib.types.str; + default = user; + }; + + mkGroupOption = + group: description: + lib.mkOption { + inherit description; + type = lib.types.str; + default = group; + }; +} From 0ca294dc176c7ef9129eb82dbfa1f2b24256d6d5 Mon Sep 17 00:00:00 2001 From: magic_rb Date: Thu, 26 Dec 2024 14:57:10 +0100 Subject: [PATCH 2/3] Add `generic-device-plugin` module Signed-off-by: magic_rb --- modules/ids.nix | 2 + modules/list.nix | 1 + modules/services/generic-device-plugin.nix | 105 +++++++++++++++++++++ overlay/default.nix | 1 + overlay/generic-device-plugin.nix | 31 ++++++ 5 files changed, 140 insertions(+) create mode 100644 modules/services/generic-device-plugin.nix create mode 100644 overlay/generic-device-plugin.nix diff --git a/modules/ids.nix b/modules/ids.nix index 46b568c..cef83f9 100644 --- a/modules/ids.nix +++ b/modules/ids.nix @@ -43,6 +43,7 @@ dnsmasq = 405; attic = 406; ntfy-sh = 407; + generic-device-plugin = 411; vmail = 5000; nobody = 65534; }; @@ -70,6 +71,7 @@ dnsmasq = 405; attic = 406; ntfy-sh = 407; + generic-device-plugin = 411; vmail = 5000; nogroup = 65534; }; diff --git a/modules/list.nix b/modules/list.nix index 9c60b6a..7d4a9cc 100644 --- a/modules/list.nix +++ b/modules/list.nix @@ -50,4 +50,5 @@ ./services/dnsmasq.nix ./services/attic.nix ./services/ntfy-sh.nix + ./services/generic-device-plugin.nix ] diff --git a/modules/services/generic-device-plugin.nix b/modules/services/generic-device-plugin.nix new file mode 100644 index 0000000..68df95e --- /dev/null +++ b/modules/services/generic-device-plugin.nix @@ -0,0 +1,105 @@ +# SPDX-FileCopyrightText: 2021 Richard Brežák and NixNG contributors +# +# SPDX-License-Identifier: MPL-2.0 +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +{ + pkgs, + config, + lib, + nglib, + ... +}: +let + cfg = config.services.genericDevicePlugin; + settingsFormat = pkgs.formats.json { }; + usageLink = "https://github.com/squat/generic-device-plugin/tree/main?tab=readme-ov-file#usage"; +in +{ + options.services.genericDevicePlugin = { + enable = lib.mkEnableOption "Whether to enable the generic-device-plugin"; + + package = lib.mkPackageOption pkgs "generic-device-plugin" { }; + + settings = { + devices = lib.mkOption { + type = lib.types.listOf (settingsFormat.type); + default = [ ]; + description = '' + See [upstream documentation](${usageLink}); + ''; + example = lib.literalExpression '' + [ + { + name = "zigbee"; + groups = lib.singleton { paths = lib.singleton { path = "/dev/ttyZigbee"; }; }; + } + { + name = "ender3"; + groups = lib.singleton { paths = lib.singleton { path = "/dev/ttyEnder3"; }; }; + } + ] + ''; + }; + domain = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = '' + See [upstream documentation](${usageLink}). If `null`, upstream default is used. + ''; + }; + listen = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = '' + See [upstream documentation](${usageLink}). If `null`, upstream default is used. + ''; + }; + logLevel = lib.mkOption { + type = lib.types.enum [ + "all" + "debug" + "info" + "warn" + "error" + "none" + "info" + ]; + default = "info"; + description = '' + Log level to use. + ''; + }; + pluginDirectory = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + description = '' + See [upstream documentation](${usageLink}). If `null`, upstream default is used. + ''; + }; + }; + user = nglib.mkUserOption "generic-device-plugin" "User to run `generic-device-plugin` as."; + group = nglib.mkGroupOption "generic-device-plugin" "Group to run `generic-device-plugin` as."; + }; + config = lib.mkIf cfg.enable { + + users.users.${cfg.user} = nglib.mkDefaultRec { + description = "generic-device-plugin"; + group = cfg.group; + createHome = false; + home = "/var/empty"; + useDefaultShell = false; + uid = config.ids.uids.generic-device-plugin; + }; + + users.groups.${cfg.group} = nglib.mkDefaultRec { gid = config.ids.gids.generic-device-plugin; }; + + init.services.generic-device-plugin = { + shutdownOnExit = true; + enabled = true; + execStart = "${lib.getExe cfg.package} --config ${settingsFormat.generate "settings.json" cfg.settings}"; + }; + }; +} diff --git a/overlay/default.nix b/overlay/default.nix index a11a682..6b92c0c 100644 --- a/overlay/default.nix +++ b/overlay/default.nix @@ -20,6 +20,7 @@ in systemdStandalone = callPackage ./systemd-minimal.nix { }; systemdTmpfilesD = callPackage ./systemd-tmpfiles.d.nix { }; dinit = callPackage ./dinit.nix { }; + generic-device-plugin = callPackage ./generic-device-plugin.nix { }; util-linuxSystemdFree = prev.util-linux.override { systemdSupport = false; diff --git a/overlay/generic-device-plugin.nix b/overlay/generic-device-plugin.nix new file mode 100644 index 0000000..f8af16a --- /dev/null +++ b/overlay/generic-device-plugin.nix @@ -0,0 +1,31 @@ +{ + buildGoModule, + fetchFromGitHub, + lib, + ... +}: + +buildGoModule { + pname = "generic-device-plugin"; + version = "20241207-unstable"; + + src = fetchFromGitHub { + owner = "squat"; + repo = "generic-device-plugin"; + rev = "36bfc606bba2064de6ede0ff2764cbb52edff70d"; + hash = "sha256-xztISJxFWKFWvanWY6WBx7KBIbJdUUkVX+YM0xLgBzk="; + }; + + vendorHash = "sha256-L0OYB6iI4z1o4FEmzpL0Qbc9uamyJZ89HWV77D10p3M="; + + # requires a docker instance running + doCheck = false; + + meta = with lib; { + description = "The generic-device-plugin enables allocating generic Linux devices, such as serial devices, the FUSE device, or video cameras, to Kubernetes Pods."; + homepage = "https://github.com/squat/generic-device-plugin"; + license = licenses.asl20; + maintainers = with maintainers; [ ]; + mainProgram = "generic-device-plugin"; + }; +} From 88b23667305f327aa45daa2e744c9421ab4c906a Mon Sep 17 00:00:00 2001 From: magic_rb Date: Thu, 26 Dec 2024 15:11:54 +0100 Subject: [PATCH 3/3] Rework `lib` Signed-off-by: magic_rb --- lib/dag.nix | 2 +- lib/default.nix | 55 ++++++++++++++++++++------------------------- lib/generators.nix | 2 +- lib/make-system.nix | 2 +- 4 files changed, 27 insertions(+), 34 deletions(-) diff --git a/lib/dag.nix b/lib/dag.nix index 2c5d7a1..298c33c 100644 --- a/lib/dag.nix +++ b/lib/dag.nix @@ -6,7 +6,7 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -{ lib }: +{ lib, ... }: import (builtins.fetchurl { url = "https://raw.githubusercontent.com/nix-community/home-manager/45abf3d38a2b51c00c347cab6950f3734e023bba/modules/lib/dag.nix"; sha256 = "sha256-NN9iKanf86D1MH9Nx8nsQj9T2+Poy9XeW9pLcZIyFHU="; diff --git a/lib/default.nix b/lib/default.nix index 6fd3b9e..0d3ae03 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,13 +1,15 @@ lib: -let - inherit (lib) types; - this = { - makeSystem = import ./make-system.nix { - nglib = this; - overlay = import ../overlay; +lib.fix ( + nglib: + let + overlay = import ../overlay; + args = { + inherit lib nglib overlay; }; - dag = import ./dag.nix { inherit lib; }; - generators = import ./generators.nix { inherit lib; }; + in + { + dag = import ./dag.nix args; + generators = import ./generators.nix args; mkDefaultRec = lib.mapAttrsRecursive (_: v: lib.mkDefault v); mkApply = fun: x: { original = x; @@ -18,31 +20,31 @@ let description: lib.mkOption { inherit description; - type = types.attrsOf ( - types.submodule { + type = lib.types.attrsOf ( + lib.types.submodule { options = { data = lib.mkOption { description = '' Script fragment which to run. ''; - type = types.str; + type = lib.types.str; }; before = lib.mkOption { description = '' Script before dependencies. See /lib/dag.nix. ''; - type = with types; listOf str; + type = lib.types.listOf lib.types.str; }; after = lib.mkOption { description = '' Script after dependencies. See /lib/dag.nix ''; - type = with types; listOf str; + type = lib.types.listOf lib.types.str; }; }; } ); - apply = this.dag.dagTopoSort; + apply = nglib.dag.dagTopoSort; default = { }; }; @@ -64,10 +66,7 @@ let )} ''; - nottmpfiles = import ./nottmpfiles { - inherit lib; - nglib = this; - }; + nottmpfiles = import ./nottmpfiles args; maybeChangeUserAndGroup = user: group: script: @@ -75,16 +74,10 @@ let "chpst -u ${user}${lib.optionalString (group != null) ":${group}"} ${script}" else script; - }; -in -this -// { - inherit - (import ./options.nix { - inherit lib; - nglib = this; - }) - mkUserOption - mkGroupOption - ; -} + + inherit (import ./options.nix args) mkUserOption mkGroupOption; + + makeSystem = import ./make-system.nix { inherit lib nglib overlay; }; + + } +) diff --git a/lib/generators.nix b/lib/generators.nix index 551241e..451cd96 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -6,7 +6,7 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -{ lib }: +{ lib, ... }: let inherit (lib) isAttrs diff --git a/lib/make-system.nix b/lib/make-system.nix index 1f8ae23..a5bd2aa 100644 --- a/lib/make-system.nix +++ b/lib/make-system.nix @@ -7,7 +7,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. # These arguments are provided by the overarching NixNG repository and are not user confugurable. -{ nglib, overlay }: +{ nglib, overlay, ... }: # These arguments are user configurable { nixpkgs,