{ config, options, lib, home-manager, ... }:

with lib;
with lib.my;
{
  options = with types; {
    user = mkOpt attrs {};

    hm = mkOpt attrs {};

    env = mkOption {
      # env = { PATH = ["$PATH" ./test]; TEST = "test"; }
      type = attrsOf (oneOf [ str path (listOf (either str path)) ]);
      # env = { PATH = "$PATH:./test"; TEST = "test"; }
      apply = mapAttrs
        (n: v: if isList v
               then concatMapStringsSep ":" (x: toString x) v
               else (toString v));
      default = {};
      description = "Set environment variables that will be joined by a colon";
    };
  };

  config = {
    user = {
      description = "The primary user account";
      extraGroups = [ "wheel" "networkmanager" ];
      isNormalUser = true;
      home = "/home/${config.user.name}";
      group = "users";
      uid = 1000;
    };

    users.users.${config.user.name} = mkAliasDefinitions options.user;

    nix.settings = let users = [ "root" config.user.name ]; in {
      trusted-users = users;
      allowed-users = users;
    };

    hm.home.stateVersion = config.system.stateVersion;

    home-manager = {
      # extraSpecialArgs = { inherit inputs; };
      useGlobalPkgs = true;
      useUserPackages = true;

      users.${config.user.name} = mkAliasDefinitions options.hm;
    };

    env.PATH = [ "$PATH" ];

    environment.extraInit =
      concatStringsSep "\n"
        (mapAttrsToList (n: v: "export ${n}=\"${v}\"") config.env);
  };
}
