Refactor to a module system

This commit is contained in:
Ehmry - 2022-06-22 20:32:09 -05:00
parent 1a18b5abdd
commit 6243c33736
9 changed files with 222 additions and 106 deletions

102
README.md
View File

@ -24,38 +24,43 @@ firmware.
```nix ```nix
let let
pkgs = import <nixpkgs> {};
# use fetchurl, Hydra inputs, or something else to refer to this project
openwrt-imagebuilder = ../nix-openwrt-imagebuilder; openwrt-imagebuilder = ../nix-openwrt-imagebuilder;
profiles = import (openwrt-imagebuilder + "/profiles.nix") { inherit pkgs; }; openwrtSystem = import (openwrt-imagebuilder + "/lib/openwrt-system.nix");
# example: find target/variant for an old Fritzbox sys = openwrtSystem {
config = profiles.identifyProfile "avm_fritz7412" // { pkgs = import <nixpkgs> { };
# add package to include in the image, ie. packages that you don't modules = [
# want to install manually later ({ pkgs, ... }: {
packages = [ "tcpdump" ];
disabledServices = [ "dnsmasq" ]; # find target/variant for an old Fritzbox
system.profile = "avm_fritz7412";
# include files in the images. # add package to include in the image, ie. packages that you don't
# to set UCI configuration, create a uci-defauts scripts as per # want to install manually later
# official OpenWRT ImageBuilder recommendation. packages = [ "tcpdump" ];
files = pkgs.runCommandNoCC "image-files" {} ''
mkdir -p $out/etc/uci-defaults services.disabled = [ "dnsmasq" ];
cat > $out/etc/uci-defaults/99-custom <<EOF
uci -q batch << EOI # include files in the images.
set system.@system[0].hostname='testap' # to set UCI configuration, create a uci-defauts scripts as per
commit # official OpenWRT ImageBuilder recommendation.
EOI files = pkgs.runCommandNoCC "image-files" { } ''
EOF mkdir -p $out/etc/uci-defaults
''; cat > $out/etc/uci-defaults/99-custom <<EOF
uci -q batch << EOI
set system.@system[0].hostname='testap'
commit
EOI
EOF
'';
})
];
}; };
in
# actually build the image # actually build the image
import (openwrt-imagebuilder + "/builder.nix") config in sys.config.system.build.image
``` ```
## Usage with Nix Flakes ## Usage with Nix Flakes
@ -66,35 +71,36 @@ in
openwrt-imagebuilder.url = "github:astro/nix-openwrt-imagebuilder"; openwrt-imagebuilder.url = "github:astro/nix-openwrt-imagebuilder";
}; };
outputs = { self, nixpkgs, openwrt-imagebuilder }: { outputs = { self, nixpkgs, openwrt-imagebuilder }: {
packages.x86_64-linux.my-router = packages.x86_64-linux.my-router = let
let sys = openwrt-imagebuilder.lib.openwrtSystem {
pkgs = nixpkgs.legacyPackages.x86_64-linux; pkgs = nixpkgs.legacyPackages.x86_64-linux;
modules = [
({ pkgs, ... }: {
system.profile = "avm_fritz7412";
profiles = openwrt-imagebuilder.lib.profiles { inherit pkgs; }; # add package to include in the image, ie. packages that you don't
# want to install manually later
packages = [ "tcpdump" ];
config = profiles.identifyProfile "avm_fritz7412" // { services.disabled = [ "dnsmasq" ];
# add package to include in the image, ie. packages that you don't
# want to install manually later
packages = [ "tcpdump" ];
disabledServices = [ "dnsmasq" ]; # include files in the images.
# to set UCI configuration, create a uci-defauts scripts as per
# official OpenWRT ImageBuilder recommendation.
files = pkgs.runCommandNoCC "image-files" { } ''
mkdir -p $out/etc/uci-defaults
cat > $out/etc/uci-defaults/99-custom <<EOF
uci -q batch << EOI
set system.@system[0].hostname='testap'
commit
EOI
EOF
'';
# include files in the images. })
# to set UCI configuration, create a uci-defauts scripts as per ];
# official OpenWRT ImageBuilder recommendation. };
files = pkgs.runCommandNoCC "image-files" {} '' in sys.config.system.build.image;
mkdir -p $out/etc/uci-defaults
cat > $out/etc/uci-defaults/99-custom <<EOF
uci -q batch << EOI
set system.@system[0].hostname='testap'
commit
EOI
EOF
'';
};
in
openwrt-imagebuilder.lib.build config;
}; };
} }
``` ```

View File

@ -1,23 +1,18 @@
{ pkgs ? import <nixpkgs> {} { pkgs, ... }:
, profiles ? import ./profiles.nix { inherit pkgs; }
, build ? import ./builder.nix {
}: system.profile = "avm_fritz7412";
build (
profiles.identifyProfile "avm_fritz7412" packages = [ "tcpdump" "vxlan" "kmod-vxlan" ];
//
{ files = pkgs.runCommandNoCC "image-files" { } ''
packages = [ mkdir -p $out/etc/uci-defaults
"tcpdump" cat > $out/etc/uci-defaults/99-custom <<EOF
"vxlan" "kmod-vxlan" uci -q batch << EOI
]; set system.@system[0].hostname='testap'
files = pkgs.runCommandNoCC "image-files" {} '' commit
mkdir -p $out/etc/uci-defaults EOI
cat > $out/etc/uci-defaults/99-custom <<EOF EOF
uci -q batch << EOI '';
set system.@system[0].hostname='testap'
commit }
EOI
EOF
'';
}
)

View File

@ -1,46 +1,39 @@
{ {
description = "Build OpenWRT images in Nix derivations"; description = "Build OpenWRT images in Nix derivations";
outputs = { self, nixpkgs }@inputs: { outputs = { self, nixpkgs }@inputs:
let pkgs' = nixpkgs.legacyPackages.x86_64-linux;
in {
lib.build = lib = {
{ pkgs ? nixpkgs.legacyPackages.x86_64-linux build = { pkgs ? pkgs', ... }@args:
, ... import ./builder.nix (args // { inherit pkgs; });
}@args:
import ./builder.nix (args // {
inherit pkgs;
});
lib.profiles = profiles = { pkgs ? pkgs', release ? "21.02.3", ... }@args:
{ pkgs ? nixpkgs.legacyPackages.x86_64-linux import ./profiles.nix (args // { inherit pkgs release; });
, release ? "21.02.3"
, ...
}@args:
import ./profiles.nix (args // {
inherit pkgs release;
});
packages.x86_64-linux.profiles-list = import ./profiles-list.nix { openwrtSystem = import ./lib/openwrt-system.nix;
pkgs = nixpkgs.legacyPackages.x86_64-linux;
};
# `nix run .#generate-hashes`
packages.x86_64-linux.generate-hashes = import ./generate-hashes.nix {
pkgs = nixpkgs.legacyPackages.x86_64-linux;
};
packages.x86_64-linux.example-image = import ./example.nix {
pkgs = nixpkgs.legacyPackages.x86_64-linux;
profiles = self.lib.profiles {
pkgs = nixpkgs.legacyPackages.x86_64-linux;
}; };
build = self.lib.build;
};
checks = self.packages; packages.x86_64-linux.profiles-list = import ./profiles-list.nix {
pkgs = pkgs';
};
hydraJobs = { # `nix run .#generate-hashes`
example-image = nixpkgs.lib.hydraJob self.packages.x86_64-linux.example-image; packages.x86_64-linux.generate-hashes = import ./generate-hashes.nix {
pkgs = pkgs';
};
packages.x86_64-linux.example-image = self.lib.openwrtSystem {
pkgs = pkgs';
modules = [ ./example.nix ];
};
checks = self.packages;
hydraJobs = {
example-image =
nixpkgs.lib.hydraJob self.packages.x86_64-linux.example-image;
};
}; };
};
} }

10
lib/modules/build.nix Normal file
View File

@ -0,0 +1,10 @@
{ config, lib, pkgs, profiles, ... }:
{
system.build.image = import ../../builder.nix {
inherit pkgs;
inherit (config.system) release target variant profile;
inherit (config) packages files;
disabledServices = config.services.disabled;
};
}

21
lib/modules/files.nix Normal file
View File

@ -0,0 +1,21 @@
{ lib, ... }:
{
options.files = with lib;
mkOption {
type = types.path;
default = null;
example = literalExample ''
pkgs.runCommandNoCC "image-files" { } '''
mkdir -p $out/etc/uci-defaults
cat > $out/etc/uci-defaults/99-custom <<EOF
uci -q batch << EOI
set system.@system[0].hostname='testap'
commit
EOI
EOF
''';
'';
description = "Directory of files to included in images.";
};
}

10
lib/modules/packages.nix Normal file
View File

@ -0,0 +1,10 @@
{ lib, ... }:
{
options.packages = with lib; mkOption {
type = with types; listOf str;
default = [ ];
example = [ "tcpdump" "vxlan" "kmod-vxlan" ];
description = "Extra OpenWRT packages (can be prefixed with "-").";
};
}

10
lib/modules/services.nix Normal file
View File

@ -0,0 +1,10 @@
{ lib, ... }:
{
options.services.disabled = with lib; mkOption {
type = with types; listOf str;
default = [ ];
example = [ "dropbear" ];
description = "Which services in /etc/init.d/ should be disabled.";
};
}

49
lib/modules/system.nix Normal file
View File

@ -0,0 +1,49 @@
{ config, lib, pkgs, profiles, ... }:
let cfg = config.system;
in {
options.system = with lib;
let nullOrStr = types.nullOr types.str;
in {
build = mkOption {
default = { };
description = ''
Attribute set of derivations used to construct the system.
'';
type = types.submoduleWith {
modules =
[{ freeformType = with types; lazyAttrsOf (uniq unspecified); }];
};
};
profile = mkOption {
type = nullOrStr;
default = null;
description = "Hardware profile";
};
release = mkOption {
type = types.str;
default = "21.02.3";
description = "OpenWRT release";
};
target = mkOption {
type = types.str;
description = "OpenWRT target";
};
variant = mkOption {
type = types.str;
description = "Hardware variant";
};
};
config.system = lib.mkIf (cfg.profile != null)
(lib.mapAttrs (_: lib.mkDefault) {
inherit (profiles.identifyProfile cfg.profile) target variant;
});
}

22
lib/openwrt-system.nix Normal file
View File

@ -0,0 +1,22 @@
{ pkgs ? import <nixpkgs> { }, modules }:
let
result = pkgs.lib.evalModules {
modules = [
({ config, ... }: {
config._module.args = {
inherit pkgs;
profiles = import ../profiles.nix {
inherit pkgs;
inherit (config.system) release;
};
};
})
./modules/system.nix
./modules/files.nix
./modules/packages.nix
./modules/services.nix
./modules/build.nix
] ++ modules;
};
in { inherit (result) config options; }