feat: working module packages

This commit is contained in:
Jake Hamilton 2024-06-14 01:26:16 -07:00
parent 856b88321e
commit ea0ed58a7e
Signed by untrusted user: jakehamilton
GPG key ID: 9762169A1B35EA68
5 changed files with 469 additions and 439 deletions

View file

@ -360,6 +360,19 @@ lib: {
## @type Attrs ## @type Attrs
port = lib.types.ints.u16; port = lib.types.ints.u16;
## A type that allows a string value specifying a version number.
##
## @type Attrs
version = lib.types.create {
name = "Version";
description = "version";
check = value: let
parts = builtins.splitVersion value;
in
builtins.isString value && builtins.length parts > 0;
merge = lib.options.merge.equal;
};
## A type that allows a string value. The merged definitions must all be ## A type that allows a string value. The merged definitions must all be
## the same. ## the same.
## ##
@ -1038,72 +1051,72 @@ lib: {
inherit initial final; inherit initial final;
}; };
}; };
};
dag = { dag = {
## Create a type that allows a DAG (Directed Acyclic Graph) of a given type. ## Create a type that allows a DAG (Directed Acyclic Graph) of a given type.
## ##
## @type Attrs -> Attrs ## @type Attrs -> Attrs
of = type: let of = type: let
resolved = lib.types.attrs.of (lib.types.dag.entry type); resolved = lib.types.attrs.of (lib.types.dag.entry type);
in
lib.types.create {
name = "Dag";
description = "Dag of ${type.description}";
check = resolved.check;
merge = resolved.merge;
fallback = resolved.fallback;
getSubOptions = prefix: type.getSubOptions (prefix ++ ["<name>"]);
getSubModules = type.getSubModules;
withSubModules = modules: lib.types.dag.of (type.withSubModules modules);
functor = lib.types.functor "dag.of" // {wrapped = type;};
children = {
element = type;
resolved = resolved;
};
};
## Create a type that allows a DAG entry of a given type.
##
## @type Attrs -> Attrs
entry = type: let
submodule = lib.types.submodule ({name}: {
options = {
value = lib.options.create {
type = type;
};
before = lib.options.create {
type = lib.types.list.of lib.types.string;
};
after = lib.options.create {
type = lib.types.list.of lib.types.string;
};
};
});
normalize = definition: let
value =
if definition ? priority
then lib.modules.order definition.priority definition.value
else definition.value;
in in
if lib.dag.validate.entry definition.value lib.types.create {
then definition.value name = "Dag";
else lib.dag.entry.anywhere value; description = "Dag of ${type.description}";
in check = resolved.check;
lib.types.create { merge = resolved.merge;
name = "DagEntry"; fallback = resolved.fallback;
description = "DagEntry (${type.description})"; getSubOptions = prefix: type.getSubOptions (prefix ++ ["<name>"]);
merge = location: definitions: getSubModules = type.getSubModules;
submodule.merge withSubModules = modules: lib.types.dag.of (type.withSubModules modules);
location functor = lib.types.functor "dag.of" // {wrapped = type;};
( children = {
builtins.map element = type;
(definition: { resolved = resolved;
__file__ = definition.__file__; };
value = normalize definition; };
})
definitions ## Create a type that allows a DAG entry of a given type.
); ##
}; ## @type Attrs -> Attrs
entry = type: let
submodule = lib.types.submodule ({name}: {
options = {
value = lib.options.create {
type = type;
};
before = lib.options.create {
type = lib.types.list.of lib.types.string;
};
after = lib.options.create {
type = lib.types.list.of lib.types.string;
};
};
});
normalize = definition: let
value =
if definition ? priority
then lib.modules.order definition.priority definition.value
else definition.value;
in
if lib.dag.validate.entry definition.value
then definition.value
else lib.dag.entry.anywhere value;
in
lib.types.create {
name = "DagEntry";
description = "DagEntry (${type.description})";
merge = location: definitions:
submodule.merge
location
(
builtins.map
(definition: {
__file__ = definition.__file__;
value = normalize definition;
})
definitions
);
};
};
}; };
} }

View file

@ -10,22 +10,22 @@
++ [ ++ [
./src/export.nix ./src/export.nix
{ {
__file__ = "broken"; __file__ = ./default.nix;
# options.foundation = lib.options.create { # options.foundation = lib.options.create {
# type = lib.types.attrs.of lib.types.derivation; # type = lib.types.attrs.of lib.types.derivation;
# }; # };
# config.foundation = foundation; # config.foundation = foundation;
config.packages.foundation = # config.packages.foundation =
builtins.mapAttrs (name: package: { # builtins.mapAttrs (name: package: {
name = package.name; # name = package.name;
inherit package; # inherit package;
meta = package.meta; # meta = package.meta;
}) # })
foundation; # foundation;
} }
]; ];
}; };

View file

@ -8,10 +8,10 @@
}, },
"locked": { "locked": {
"dir": "foundation", "dir": "foundation",
"dirtyRev": "9c29945531c58ad81f05cd1f4958c8894a733216-dirty", "dirtyRev": "856b88321e5f19019332f8b60b729095c2260340-dirty",
"dirtyShortRev": "9c29945-dirty", "dirtyShortRev": "856b883-dirty",
"lastModified": 1718255029, "lastModified": 1718299377,
"narHash": "sha256-fmrDe4GfvVfXZ9lzaOt+tgBUMFCsyKr0Dlnm8aQwAXs=", "narHash": "sha256-L6zriSSFi45uJ8u3HVsZ6iDHjNtQBB+aDWQfiReoLkM=",
"type": "git", "type": "git",
"url": "file:../?dir=foundation" "url": "file:../?dir=foundation"
}, },
@ -24,10 +24,10 @@
"lib": { "lib": {
"locked": { "locked": {
"dir": "lib", "dir": "lib",
"dirtyRev": "9c29945531c58ad81f05cd1f4958c8894a733216-dirty", "dirtyRev": "856b88321e5f19019332f8b60b729095c2260340-dirty",
"dirtyShortRev": "9c29945-dirty", "dirtyShortRev": "856b883-dirty",
"lastModified": 1718255029, "lastModified": 1718299377,
"narHash": "sha256-fmrDe4GfvVfXZ9lzaOt+tgBUMFCsyKr0Dlnm8aQwAXs=", "narHash": "sha256-L6zriSSFi45uJ8u3HVsZ6iDHjNtQBB+aDWQfiReoLkM=",
"type": "git", "type": "git",
"url": "file:../?dir=lib" "url": "file:../?dir=lib"
}, },

View file

@ -71,7 +71,8 @@ in {
}; };
license = lib.options.create { license = lib.options.create {
type = config.lib.types.license; type = lib.types.nullish config.lib.types.license;
default.value = null;
description = "The license for the package."; description = "The license for the package.";
}; };
@ -107,362 +108,345 @@ in {
}; };
}; };
package = {
base = lib.types.submodule ({config}: {
freeform = lib.types.any;
options = {
name = lib.options.create {
type = lib.types.string;
default = {
value = "${config.pname}-${config.version or "unknown"}";
text = "\${config.pname}-\${config.version}";
};
description = "The name of the package.";
};
pname = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
description = "The name of the package.";
};
version = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
description = "The version of the package.";
};
meta = lib.options.create {
type = lib'.types.meta;
default = {
text = "{ name = <package>.pname; }";
value = {
name = config.pname;
};
};
description = "The metadata for the package.";
};
env = lib.options.create {
type = lib.types.attrs.of lib.types.string;
default.value = {};
description = "Environment variables for the package's builder to use.";
};
phases = lib.options.create {
type = lib.types.dag.of (
lib.types.either
lib.types.string
(lib.types.function lib.types.string)
);
default.value = {};
description = "Phases for the package's builder to use.";
};
platform = {
build = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
description = "The platform the package is built on.";
};
host = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
description = "The platform the package is run on.";
};
target = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
description = "The platform the package generates code for.";
};
};
builder = lib.options.create {
type = lib'.types.builder;
description = "The builder for the package.";
};
package = lib.options.create {
type = lib.types.derivation;
default = {
value = config.builder.build config.builder config;
text = "<derivation>";
};
description = "The package derivation.";
};
deps = {
build = {
only = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are only used in the build environment.";
};
build = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are created in the build environment and are run in the build environment.";
};
host = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are created in the build environment and are run in the host environment.";
};
target = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are created in the build environment and are run in the target environment.";
};
};
host = {
only = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are only used in the host environment.";
};
host = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are run in the host environment.";
};
target = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are run in the host environment which produces code for the target environment.";
};
};
target = {
only = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are only used in the target environment.";
};
target = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are run in the target environment.";
};
};
};
versions = lib.options.create {
type = lib.types.attrs.of lib'.types.package;
default.value = {};
description = "Available versions of the package.";
};
};
});
targeted = lib.types.submodule ({config}: {
freeform = lib.types.any;
options = {
name = lib.options.create {
type = lib.types.string;
default = {
value = "${config.pname}-${config.version or "unknown"}";
text = "\${config.pname}-\${config.version}";
};
description = "The name of the package.";
};
pname = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
description = "The name of the package.";
};
version = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
description = "The version of the package.";
};
meta = lib.options.create {
type = lib'.types.meta;
default = {
text = "{ name = <package>.pname; }";
value = {
name = config.pname;
};
};
description = "The metadata for the package.";
};
env = lib.options.create {
type = lib.types.attrs.of lib.types.string;
default.value = {};
description = "Environment variables for the package's builder to use.";
};
phases = lib.options.create {
type = lib.types.dag.of (
lib.types.either
lib.types.string
(lib.types.function lib.types.string)
);
default.value = {};
description = "Phases for the package's builder to use.";
};
platform = {
build = lib.options.create {
type = lib.types.string;
description = "The platform the package is built on.";
};
host = lib.options.create {
type = lib.types.string;
description = "The platform the package is run on.";
};
target = lib.options.create {
type = lib.types.string;
description = "The platform the package generates code for.";
};
};
builder = lib.options.create {
type = lib'.types.builder;
description = "The builder for the package.";
};
package = lib.options.create {
type = lib.types.derivation;
default = {
value = config.builder.build config.builder config;
text = "<derivation>";
};
description = "The package derivation.";
};
deps = {
build = {
only = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are only used in the build environment.";
};
build = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are created in the build environment and are run in the build environment.";
};
host = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are created in the build environment and are run in the host environment.";
};
target = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are created in the build environment and are run in the target environment.";
};
};
host = {
only = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are only used in the host environment.";
};
host = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are run in the host environment.";
};
target = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are run in the host environment which produces code for the target environment.";
};
};
target = {
only = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are only used in the target environment.";
};
target = lib.options.create {
type = lib'.types.dependencies;
default.value = {};
description = "Dependencies which are run in the target environment.";
};
};
};
versions = lib.options.create {
type = lib.types.attrs.of lib'.types.package;
default.value = {};
description = "Available versions of the package.";
};
};
});
};
dependencies = lib.types.attrs.of (lib.types.nullish lib'.types.package);
packages = {
base = lib.types.attrs.of (lib.types.submodule {
freeform = lib'.types.package.base;
# options =
# builtins.foldl' (result: system: {
# "${system}" = lib.options.create {
# type = lib'.types.packages.targeted;
# default.value = {};
# description = "Packages for the architecture `${system}`";
# };
# }) {}
# lib'.systems.doubles.all;
});
targeted = lib.types.attrs.of (lib.types.submodule {
freeform = lib.types.nullish lib'.types.package.targeted;
options = {
cross = lib.options.create {
type = lib'.types.packages.cross;
default.value = {};
description = "Cross-compiled packages targeting another architecture.";
};
};
});
# Cross-compiled packages are accessed via
# packages.cross.<platform>.<namespace>.<name>
cross = lib.types.attrs.of (lib.types.submodule {
freeform = lib.types.attrs.of (lib.types.submodule {
freeform = lib.types.nullish lib'.types.package.targeted;
});
});
};
builder = lib.types.submodule { builder = lib.types.submodule {
freeform = lib.types.any; freeform = lib.types.any;
options = { options = {
build = lib.options.create { build = lib.options.create {
type = lib.types.function lib.types.derivation; type = lib.types.function lib.types.derivation;
description = "The function that creates the package derivation."; };
};
};
packages = {
generic = lib.types.attrs.of (lib.types.submodule ({name}: {
freeform = lib.types.attrs.of (lib.types.submodule [
lib'.types.package.generic'
]);
}));
# packages.<system>
targeted = lib.types.attrs.of (lib.types.submodule ({name}: let
system = name;
in {
# packages.<system>.<namespace>
freeform = lib.types.attrs.of (lib.types.submodule ({name}: let
namespace = name;
in {
# packages.<system>.<namespace>.<name>
freeform = lib.types.attrs.of (lib.types.submodule [
lib'.types.package.targeted'
{
config = {
namespace = namespace;
platform = {
build = system;
host = lib.modules.overrides.default system;
target = lib.modules.overrides.default system;
};
};
}
]);
}));
# packages.<system>.<cross>
options.cross = lib.attrs.generate lib'.systems.doubles.all (cross:
lib.options.create {
default.value = {};
# packages.<system>.<cross>.<namespace>
type = lib.types.attrs.of (lib.types.submodule (
{name}: let
namespace = name;
in {
# packages.<system>.<cross>.<namespace>.<name>
freeform = lib.types.attrs.of (lib.types.submodule [
lib'.types.package.targeted'
{
config = {
namespace = namespace;
platform = {
build = system;
host = cross;
target = lib.modules.overrides.default cross;
};
};
}
]);
}
));
});
}));
};
dependencies = {
generic = lib.types.attrs.of (lib.types.nullish lib'.types.package.generic);
targeted = lib.types.attrs.of (lib.types.nullish lib'.types.package.targeted);
};
package = {
generic = lib.types.submodule lib'.types.package.generic';
generic' = args @ {
name ? assert false; null,
config,
}: {
options = {
pname = lib.options.create {
type = lib.types.string;
default = {
text = "<name> or \"unknown\"";
value =
if args ? name
then args.name
else "unknown";
};
};
version = lib.options.create {
type = lib.types.nullish lib.types.version;
default.value = null;
};
meta = lib.options.create {
type = lib'.types.meta;
default.value = {
name = config.pname;
};
};
phases = lib.options.create {
type = lib.types.dag.of (
lib.types.either
lib.types.string
(lib.types.function lib.types.string)
);
default.value = {};
};
env = lib.options.create {
type = lib.types.attrs.of lib.types.string;
default.value = {};
};
builder = lib.options.create {
type = lib'.types.builder;
};
deps = {
build = {
only = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
build = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
host = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
target = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
};
host = {
only = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
host = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
target = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
};
target = {
only = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
target = lib.options.create {
type = lib'.types.dependencies.generic;
default.value = {};
};
};
};
versions = lib.options.create {
type = lib.types.attrs.of lib'.types.packages.generic;
default.value = {};
};
};
};
targeted = lib.types.submodule lib'.types.package.targeted';
targeted' = args @ {
name ? assert false; null,
config,
}: {
options = {
name = lib.options.create {
type = lib.types.string;
default = {
text = "\${namespace}-\${pname}-\${version} or \${pname}-\${version}";
value = let
namespace =
if config.namespace == null
then ""
else "${config.namespace}-";
version =
if config.version == null
then ""
else "-${config.version}";
in "${namespace}${config.pname}${version}";
};
};
namespace = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
};
pname = lib.options.create {
type = lib.types.string;
default = {
text = "<name> or \"unknown\"";
value =
if args ? name
then args.name
else "unknown";
};
};
version = lib.options.create {
type = lib.types.nullish lib.types.version;
default.value = null;
};
meta = lib.options.create {
type = lib'.types.meta;
default.value = {
name = config.pname;
};
};
platform = {
build = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
};
host = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
};
target = lib.options.create {
type = lib.types.nullish lib.types.string;
default.value = null;
};
};
phases = lib.options.create {
type = lib.types.dag.of (
lib.types.either
lib.types.string
(lib.types.function lib.types.string)
);
default.value = {};
};
env = lib.options.create {
type = lib.types.attrs.of lib.types.string;
default.value = {};
};
builder = lib.options.create {
type = lib'.types.builder;
};
package = lib.options.create {
type = lib.types.derivation;
};
deps = {
build = {
only = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
build = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
host = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
target = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
};
host = {
only = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
host = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
target = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
};
target = {
only = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
target = lib.options.create {
type = lib'.types.dependencies.targeted;
default.value = {};
};
};
};
versions = lib.options.create {
type = lib.types.attrs.of lib'.types.packages.targeted;
default.value = {};
};
};
config = {
package = config.builder.build config;
}; };
}; };
}; };

View file

@ -6,22 +6,55 @@
doubles = lib'.systems.doubles.all; doubles = lib'.systems.doubles.all;
generic = builtins.removeAttrs config.packages ["targeted"]; generic = config.packages.generic;
targeted = {
i686-linux = generic;
};
in { in {
includes = [ includes = [
# ./aux/foundation.nix # ./aux/foundation.nix
]; ];
options = { options = {
packages = lib.options.create { packages = {
default.value = {}; generic = lib.options.create {
type = lib.types.attrs.of (lib.types.submodule { type = lib'.types.packages.generic;
freeform = lib.types.any; default.value = {};
}); };
targeted = lib.options.create {
type = lib'.types.packages.targeted;
};
}; };
}; };
config = { config = {
packages.targeted.i686-linux = generic; packages = {
generic = {
example = {
x = {
version = "1.0.0";
builder.build = package:
derivation {
name = package.name;
builder = "/bin/sh";
system = package.platform.build;
};
phases = {
build = ''
make
'';
install = lib.dag.entry.after ["build"] ''
make install DESTDIR=$out
'';
};
};
};
};
inherit targeted;
};
}; };
} }