Skip to content

Cannot import a custom nixosModule in a separate file #274

Open
@TakodaS

Description

@TakodaS

I am unable to import custom modules from a local file and use them in my own nixosConfigurations. For some reason the moduleWithSystem argument is not passed to paths under flake.imports even though the documentation would lead me to believe this is the case.

nix build .#nixosConfigurations.mymodule1.config.system.build.toplevel give me error: attribute 'moduleWithSystem' missing
nix build .#nixosConfigurations.mymodule2.config.system.build.toplevel works, only erroring out later because the configuration is not bootable.

# flake.nix
{
  description = "A Nix-flake-based Nix development environment";

  outputs =
    inputs@{
      self,
      nixpkgs,
      flake-parts,
      ...
    }:
    flake-parts.lib.mkFlake { inherit inputs; } (
      top@{ moduleWithSystem, withSystem, ... }:
      {
        systems = nixpkgs.lib.systems.flakeExposed;
        flake = {
          imports = [
            ./mymodule1.nix
          ];
          nixosModules.mymodule2 = moduleWithSystem (
            perSystem@{ config }: # NOTE: only explicit params will be in perSystem
            nixos@{ pkgs, ... }:
            {
              services.httpd.enable = true;
            }
          );
          nixosConfigurations =
            let
              inherit (nixpkgs.lib.attrsets) mapAttrs;
            in
            mapAttrs (
              _name: value:
              nixpkgs.lib.nixosSystem {
                system = "x86_64-linux";
                modules = [
                  value
                ];
              }
            ) self.nixosModules;

        };
      }
    );
}
# ./mymodule1.nix
{ moduleWithSystem, ... }:
{
  nixosModules.mymodule1 = moduleWithSystem (
    perSystem@{ config }: # NOTE: only explicit params will be in perSystem
    nixos@{ ... }:
    {
      services.httpd.enable = true;
    }
  );
}

EDIT: as a bonus, it would be nice do something like

# flake.nix
nixosModules.mymodule3 = importWithSystem ./mymodule3.nix

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions