Refactor to a module system
This commit is contained in:
parent
1a18b5abdd
commit
6243c33736
46
README.md
46
README.md
|
@ -24,25 +24,28 @@ firmware.
|
|||
|
||||
```nix
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
|
||||
# use fetchurl, Hydra inputs, or something else to refer to this project
|
||||
openwrt-imagebuilder = ../nix-openwrt-imagebuilder;
|
||||
|
||||
profiles = import (openwrt-imagebuilder + "/profiles.nix") { inherit pkgs; };
|
||||
openwrtSystem = import (openwrt-imagebuilder + "/lib/openwrt-system.nix");
|
||||
|
||||
sys = openwrtSystem {
|
||||
pkgs = import <nixpkgs> { };
|
||||
modules = [
|
||||
({ pkgs, ... }: {
|
||||
|
||||
# find target/variant for an old Fritzbox
|
||||
system.profile = "avm_fritz7412";
|
||||
|
||||
# example: find target/variant for an old Fritzbox
|
||||
config = profiles.identifyProfile "avm_fritz7412" // {
|
||||
# add package to include in the image, ie. packages that you don't
|
||||
# want to install manually later
|
||||
packages = [ "tcpdump" ];
|
||||
|
||||
disabledServices = [ "dnsmasq" ];
|
||||
services.disabled = [ "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" {} ''
|
||||
files = pkgs.runCommandNoCC "image-files" { } ''
|
||||
mkdir -p $out/etc/uci-defaults
|
||||
cat > $out/etc/uci-defaults/99-custom <<EOF
|
||||
uci -q batch << EOI
|
||||
|
@ -51,11 +54,13 @@ let
|
|||
EOI
|
||||
EOF
|
||||
'';
|
||||
|
||||
})
|
||||
];
|
||||
};
|
||||
|
||||
in
|
||||
# actually build the image
|
||||
import (openwrt-imagebuilder + "/builder.nix") config
|
||||
in sys.config.system.build.image
|
||||
```
|
||||
|
||||
## Usage with Nix Flakes
|
||||
|
@ -66,23 +71,23 @@ in
|
|||
openwrt-imagebuilder.url = "github:astro/nix-openwrt-imagebuilder";
|
||||
};
|
||||
outputs = { self, nixpkgs, openwrt-imagebuilder }: {
|
||||
packages.x86_64-linux.my-router =
|
||||
let
|
||||
packages.x86_64-linux.my-router = let
|
||||
sys = openwrt-imagebuilder.lib.openwrtSystem {
|
||||
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
||||
modules = [
|
||||
({ pkgs, ... }: {
|
||||
system.profile = "avm_fritz7412";
|
||||
|
||||
profiles = openwrt-imagebuilder.lib.profiles { inherit pkgs; };
|
||||
|
||||
config = profiles.identifyProfile "avm_fritz7412" // {
|
||||
# add package to include in the image, ie. packages that you don't
|
||||
# want to install manually later
|
||||
packages = [ "tcpdump" ];
|
||||
|
||||
disabledServices = [ "dnsmasq" ];
|
||||
services.disabled = [ "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" {} ''
|
||||
files = pkgs.runCommandNoCC "image-files" { } ''
|
||||
mkdir -p $out/etc/uci-defaults
|
||||
cat > $out/etc/uci-defaults/99-custom <<EOF
|
||||
uci -q batch << EOI
|
||||
|
@ -91,10 +96,11 @@ in
|
|||
EOI
|
||||
EOF
|
||||
'';
|
||||
};
|
||||
|
||||
in
|
||||
openwrt-imagebuilder.lib.build config;
|
||||
})
|
||||
];
|
||||
};
|
||||
in sys.config.system.build.image;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
|
25
example.nix
25
example.nix
|
@ -1,16 +1,11 @@
|
|||
{ pkgs ? import <nixpkgs> {}
|
||||
, profiles ? import ./profiles.nix { inherit pkgs; }
|
||||
, build ? import ./builder.nix
|
||||
}:
|
||||
build (
|
||||
profiles.identifyProfile "avm_fritz7412"
|
||||
//
|
||||
{
|
||||
packages = [
|
||||
"tcpdump"
|
||||
"vxlan" "kmod-vxlan"
|
||||
];
|
||||
files = pkgs.runCommandNoCC "image-files" {} ''
|
||||
{ pkgs, ... }:
|
||||
|
||||
{
|
||||
system.profile = "avm_fritz7412";
|
||||
|
||||
packages = [ "tcpdump" "vxlan" "kmod-vxlan" ];
|
||||
|
||||
files = pkgs.runCommandNoCC "image-files" { } ''
|
||||
mkdir -p $out/etc/uci-defaults
|
||||
cat > $out/etc/uci-defaults/99-custom <<EOF
|
||||
uci -q batch << EOI
|
||||
|
@ -19,5 +14,5 @@ build (
|
|||
EOI
|
||||
EOF
|
||||
'';
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
|
|
43
flake.nix
43
flake.nix
|
@ -1,46 +1,39 @@
|
|||
{
|
||||
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 =
|
||||
{ pkgs ? nixpkgs.legacyPackages.x86_64-linux
|
||||
, ...
|
||||
}@args:
|
||||
import ./builder.nix (args // {
|
||||
inherit pkgs;
|
||||
});
|
||||
lib = {
|
||||
build = { pkgs ? pkgs', ... }@args:
|
||||
import ./builder.nix (args // { inherit pkgs; });
|
||||
|
||||
lib.profiles =
|
||||
{ pkgs ? nixpkgs.legacyPackages.x86_64-linux
|
||||
, release ? "21.02.3"
|
||||
, ...
|
||||
}@args:
|
||||
import ./profiles.nix (args // {
|
||||
inherit pkgs release;
|
||||
});
|
||||
profiles = { pkgs ? pkgs', release ? "21.02.3", ... }@args:
|
||||
import ./profiles.nix (args // { inherit pkgs release; });
|
||||
|
||||
openwrtSystem = import ./lib/openwrt-system.nix;
|
||||
};
|
||||
|
||||
packages.x86_64-linux.profiles-list = import ./profiles-list.nix {
|
||||
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
||||
pkgs = pkgs';
|
||||
};
|
||||
|
||||
# `nix run .#generate-hashes`
|
||||
packages.x86_64-linux.generate-hashes = import ./generate-hashes.nix {
|
||||
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
||||
pkgs = pkgs';
|
||||
};
|
||||
|
||||
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;
|
||||
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;
|
||||
example-image =
|
||||
nixpkgs.lib.hydraJob self.packages.x86_64-linux.example-image;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
10
lib/modules/build.nix
Normal file
10
lib/modules/build.nix
Normal 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
21
lib/modules/files.nix
Normal 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
10
lib/modules/packages.nix
Normal 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
10
lib/modules/services.nix
Normal 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
49
lib/modules/system.nix
Normal 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
22
lib/openwrt-system.nix
Normal 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; }
|
Loading…
Reference in New Issue
Block a user