Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic-device-plugin package and module #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dag.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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=";
Expand Down
45 changes: 24 additions & 21 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <literal>/lib/dag.nix</literal>.
'';
type = with types; listOf str;
type = lib.types.listOf lib.types.str;
};
after = lib.mkOption {
description = ''
Script after dependencies. See <literal>/lib/dag.nix</literal>
'';
type = with types; listOf str;
type = lib.types.listOf lib.types.str;
};
};
}
);
apply = this.dag.dagTopoSort;
apply = nglib.dag.dagTopoSort;
default = { };
};

Expand All @@ -64,17 +66,18 @@ let
)}
'';

nottmpfiles = import ./nottmpfiles {
inherit lib;
nglib = this;
};
nottmpfiles = import ./nottmpfiles args;

maybeChangeUserAndGroup =
user: group: script:
if user != null then
"chpst -u ${user}${lib.optionalString (group != null) ":${group}"} ${script}"
else
script;
};
in
this

inherit (import ./options.nix args) mkUserOption mkGroupOption;

makeSystem = import ./make-system.nix { inherit lib nglib overlay; };

}
)
2 changes: 1 addition & 1 deletion lib/generators.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/make-system.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions lib/options.nix
Original file line number Diff line number Diff line change
@@ -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;
};
}
2 changes: 2 additions & 0 deletions modules/ids.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
dnsmasq = 405;
attic = 406;
ntfy-sh = 407;
generic-device-plugin = 411;
vmail = 5000;
nobody = 65534;
};
Expand Down Expand Up @@ -70,6 +71,7 @@
dnsmasq = 405;
attic = 406;
ntfy-sh = 407;
generic-device-plugin = 411;
vmail = 5000;
nogroup = 65534;
};
Expand Down
1 change: 1 addition & 0 deletions modules/list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@
./services/dnsmasq.nix
./services/attic.nix
./services/ntfy-sh.nix
./services/generic-device-plugin.nix
]
105 changes: 105 additions & 0 deletions modules/services/generic-device-plugin.nix
Original file line number Diff line number Diff line change
@@ -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.";
Copy link
Collaborator

@pizzapim pizzapim Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead use the user and group options under config.init.services.generic-device-plugin? Also I think the user and group are currently unused?

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}";
};
};
}
1 change: 1 addition & 0 deletions overlay/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions overlay/generic-device-plugin.nix
Original file line number Diff line number Diff line change
@@ -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";
};
}