~dblsaiko/nix-extras

38e5fc61baa577e9cc7341dc24b24bb664fe26d5 — Marco Rebhan 3 months ago af241b9
Improve sys2x.systemNix module
2 files changed, 35 insertions(+), 18 deletions(-)

M common/modules/sys2x/overlays.nix
M common/modules/sys2x/system-nix.nix
M common/modules/sys2x/overlays.nix => common/modules/sys2x/overlays.nix +4 -1
@@ 5,6 5,8 @@
}: let
  inherit (lib) mkOption;
  inherit (lib.types) listOf path;

  cfg = config.sys2x.overlays;
in {
  options = {
    sys2x.overlays = {


@@ 17,6 19,7 @@ in {
  };

  config = {
    nixpkgs.overlays = map import config.sys2x.overlays.paths;
    sys2x.systemNix.overlays = cfg.paths;
    nixpkgs.overlays = map import cfg.paths;
  };
}

M common/modules/sys2x/system-nix.nix => common/modules/sys2x/system-nix.nix +31 -17
@@ 1,27 1,30 @@
{
  config,
  pkgs,
  lib,
  pkgs,
  ...
}: let
  inherit (builtins) concatStringsSep;
  inherit (lib) boolToString mapAttrs mapAttrs' mkIf mkOption optionalAttrs;
  inherit (lib.types) attrs attrsOf bool;
  inherit (lib) concatStringsSep escapeShellArg escapeShellArgs genAttrs mapAttrs mapAttrs' mkIf mkOption;
  inherit (lib.types) attrs attrsOf bool listOf path str;

  inputs = cfg.flakes // optionalAttrs (cfg.patchNixpkgs && cfg.flakes ? "nixpkgs") {
    nixpkgs = patchPkgs cfg.flakes.nixpkgs;
  };
  inputs =
    cfg.flakes
    // genAttrs cfg.nixpkgsNames (k: patchPkgs cfg.flakes.${k});

  # inject our overlays by patching the input flake to always include them in
  # the overlays array passed to the nixpkgs entrypoint
  patchPkgs = nixpkgs:
    pkgs.stdenv.mkDerivation rec {
    pkgs.stdenvNoCC.mkDerivation {
      name = "source";
      src = nixpkgs;
      phases = "unpackPhase patchPhase installPhase";
      patchPhase = ''
        mkdir overlays
        ${concatStringsSep "\n" (map (path: "cp -Lrv '${path}' 'overlays/${baseNameOf path}'") config.sys2x.overlays.paths)}
        ${concatStringsSep "\n" (map (path: escapeShellArgs ["cp" "-Lrv" "${path}" "overlays/${baseNameOf path}"]) cfg.overlays)}

        # FIXME: filter out function objects?
        printf '%s' ${escapeShellArg (builtins.toJSON config.nixpkgs.config)} > config.json

        ${pkgs.buildPackages.alejandra}/bin/alejandra -q > default.nix <<EOF
          args @ {
            overlays ? [],


@@ 29,20 32,18 @@
            ...
          }:
          (
            $(cat ${src}/default.nix)
            $(cat $src/default.nix)
          )
          (args
            // {
              overlays =
                [
                  ${concatStringsSep "\n" (map (path: "(import ./overlays/${baseNameOf path})") config.sys2x.overlays.paths)}
                  ${concatStringsSep "\n" (map (path: "(import ./overlays/${baseNameOf path})") cfg.overlays)}
                ]
                ++ overlays;
              config =
                config
                // {
                  allowUnfree = ${boolToString config.nixpkgs.config.allowUnfree or false};
                };
                builtins.fromJSON (builtins.readFile ./config.json)
                // config;
            })
        EOF
      '';


@@ 64,9 65,22 @@ in {
      patchNixpkgs = mkOption {
        default = true;
        description = ''
          Whether to patch nixpkgs to load overlays set in sys2x.overlays.paths,
          and pass the allowUnfree option.
          Whether to patch nixpkgs to load overlays set in
          sys2x.systemNix.overlays, and pass the contents of nixpkgs.config.
        '';
        type = bool;
      };

      nixpkgsNames = mkOption {
        default = ["nixpkgs"];
        description = "Flakes to patch to load nixpkgs overlays.";
        type = listOf str;
      };

      overlays = mkOption {
        default = [];
        description = "Overlays to pass to system nixpkgs.";
        type = listOf path;
      };

      flakes = mkOption {