Add UCI batch and Dropbear options

This commit is contained in:
Ehmry - 2022-06-23 12:50:55 -05:00
parent 2cd689783a
commit 807ee39110
5 changed files with 64 additions and 3 deletions

View File

@ -5,6 +5,10 @@
packages.include = [ "tcpdump" "vxlan" "kmod-vxlan" ];
dropbear.settings = {
PasswordAuth = false;
};
files = pkgs.runCommandNoCC "image-files" { } ''
mkdir -p $out/etc/uci-defaults
cat > $out/etc/uci-defaults/99-custom <<EOF

16
lib/modules/dropbear.nix Normal file
View File

@ -0,0 +1,16 @@
{ config, lib, ... }:
let cfg = config.dropbear;
in {
options.dropbear.settings = with lib;
mkOption {
default = { };
type = types.uciSection;
description = ''
See <link xlink:href="https://openwrt.org/docs/guide-user/base-system/dropbear"/>'';
};
config.uci.batch = lib.toUciBatch {
config = "dropbear";
type = "dropbear";
} cfg.settings;
}

20
lib/modules/uci.nix Normal file
View File

@ -0,0 +1,20 @@
{ config, lib, ... }:
with lib;
let cfg = config.uci;
in {
options.uci.batch = mkOption {
type = types.lines;
description = "UCI script to execute on first boot.";
example = ''
set system.@system[0].hostname='testap'
set dropbear.@dropbear[0].RootPasswordAuth='off'
'';
};
config.extraFiles."/etc/uci-defaults/99-config.uci.batch".text =
mkIf (cfg.batch != "") ''
uci -q batch << NIX_EOI
${cfg.batch}
uci commit
NIX_EOI
'';
}

View File

@ -1,7 +1,8 @@
{ pkgs ? import <nixpkgs> { }, modules }:
let
result = pkgs.lib.evalModules {
lib' = pkgs.lib.extend (import ./uci.nix);
result = lib'.evalModules {
modules = [
({ config, ... }: {
config._module.args = {
@ -12,11 +13,13 @@ let
};
};
})
./modules/system.nix
./modules/build.nix
./modules/dropbear.nix
./modules/files.nix
./modules/packages.nix
./modules/services.nix
./modules/build.nix
./modules/system.nix
./modules/uci.nix
] ++ modules;
};
in { inherit (result) config options; }

18
lib/uci.nix Normal file
View File

@ -0,0 +1,18 @@
final: prev:
let lib = prev;
in {
types = lib.types
// (with lib.types; { uciSection = attrsOf (oneOf [ bool int str ]); });
toUciBatch = with builtins;
let toAtom = v: if isBool v then (if v then "0" else "1") else (toString v);
in { config, type, section ? "@${type}[0]" }:
attrs:
let
cmds = [ "set ${config}.${section}=${type}" ]
++ (lib.attrsets.mapAttrsToList (option: value:
"set ${config}.${section}.${option}='${toAtom value}'") attrs);
in lib.strings.concatStringsSep "\n" cmds;
}