# copied and adopted from maralorns config
# This automatically searches for nixos configs in ./machines/${name}/configuration.nix
# and exposes them as outputs.nixosConfigurations.${name}
{
  withSystem,
  lib,
  inputs,
  ...
}: {
  flake = {
    nixosConfigurations = withSystem "x86_64-linux" ({pkgs, ...}: let
      machines = builtins.attrNames (builtins.readDir ./machines);
      makeSystem = name: let
        importedConfig = import (./. + "/machines/${name}/configuration.nix");
        systemConfig =
          if lib.isFunction importedConfig
          then x: importedConfig (x // {flake-inputs = inputs;})
          else importedConfig;
      in
        pkgs.nixos {
          imports = [
            systemConfig
            inputs.sops-nix.nixosModules.sops
            inputs.impermanence.nixosModules.impermanence
          ];
        };
    in
      lib.genAttrs machines makeSystem);
  };
}