Portable Submodules

Portables would just be a convenience module in the Nixpkgs Module System.
It’s an instance of an application agnostic module, so that’s fun.

# extend-option.nix (or portable.nix)
{ extendModules, lib, ... }: {
  options.extend = lib.mkOption {
    description = "Extend this configuration or submodule with another module";
  };
  config.extend = module: (extendModules {
    modules = [ module ];
  }).config;
}

Usage:

# configuration.nix
{
  # EDIT: oops, posted wrong snippet 
  # services.nginx.virtualHosts."foo" = { ... }: {
    imports = [ ./extend-option.nix ];
  # };
}
$ nixos-rebuild repl
nix-repl> config.networking.hostName
"nixos"

nix-repl> c = config.extend { networking.hostName = lib.mkForce "bar"; }
nix-repl> c.networking.hostName
"bar"

I don’t feel like it makes things portable; just extensible, but maybe something flew over my head at 1 AM :sweat_smile:

I think it might be confusing if we start a module based Portable Service Layer implementation, because that would just use imports, and have no use for extendModules or this extend option iirc.

For context:

2 Likes