wrtnix/flake.nix

89 lines
3 KiB
Nix

{
description = "Configure and manage OpenWrt";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};
outputs = { self, nixpkgs }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in rec {
utils = (import ./utils.nix) pkgs;
config = utils.buildConfig ~/.config/nixwrt/config.nix;
out = pkgs.stdenvNoCC.mkDerivation {
name = "openwrt";
dontUnpack = true;
dhcp = pkgs.writeText "dhcp" config.dhcp;
# dropbear = pkgs.writeText "dropbear" config.dropbear;
firewall = pkgs.writeText "firewall" config.firewall;
network = pkgs.writeText "network" config.network;
sys = pkgs.writeText "system" config.system;
wireless = pkgs.writeText "wireless" config.wireless;
installPhase = ''
mkdir -p $out/etc/config
cd $out/etc/config
cp $dhcp dhcp
# cp $dropbear dropbear
cp $firewall firewall
cp $network network
cp $sys system
cp $wireless wireless
'';
};
# openwrtInstall = pkgs.stdenvNoCC.mkDerivation {
# name = "openwrtInstall";
# dontUnpack = true;
# installPhase = ''
# '';
# };
openwrt-rebuild = pkgs.stdenvNoCC.mkDerivation (
let rebuild = ./openwrt-rebuild.sh; in {
name = "openwrt-rebuild";
dontUnpack = true;
installPhase = "cp $src $out";
src = pkgs.writeShellScript "openwrt-rebuild" ''
ssh root@spiffyap.lan 'bash -s' < ${rebuild}
scp -r ${out}/* root@spiffyap.lan:/etc/nix-current
'';
buildInputs = with pkgs; [ openssh ];
});
packages.x86_64-linux.default = openwrt-rebuild;
nixosModules.default = { config, ... }:
with pkgs.lib;
let cfg = config.openwrt; in {
options = {
enable = mkEnableOption "Enable OpenWrt management module";
};
config = mkIf cfg.enable {
system.activationScripts.openwrtInstall = {
text = ''
ssh root@spiffyap.lan 'bash -s' < ${openwrt-rebuild}
'';
deps = [ "specialfs" ];
};
# system.activationScripts.openwrtNewGeneration = {
# text = ''
# generation="$(basename "$(readlink /var/openwrt)" || echo 0)"
# (( ++generation ))
# echo "[openwrt] creating new generation in /var/openwrt/$generation"
# mkdir -p "/var/openwrt_mnt"
# chmod 0751 "/var/openwrt_mnt"
# ${mountCommand}
# mkdir -p ""
# '';
# deps = [ "specialfs" ];
# };
# systemd.services.openwrt = {
# wantedBy = [ ];
# serviceConfig.ExecStart = "${packages.default}";
# };
};
};
};
}