forked from fediversity/fediversity
Compare commits
10 commits
0f7da57392
...
753dd0e037
| Author | SHA1 | Date | |
|---|---|---|---|
| 753dd0e037 | |||
| 37fd4d6642 | |||
| 8c571c2dbe | |||
| a102ad93b7 | |||
| 89b22df3a4 | |||
| b49e426ed4 | |||
| 421d7b6f93 | |||
| bf488f89e1 | |||
| 7066b2cb69 | |||
| 243ef4425b |
3 changed files with 119 additions and 65 deletions
|
|
@ -43,11 +43,9 @@ in
|
|||
extra-config = mkOption {
|
||||
description = "Any options from NixOS";
|
||||
};
|
||||
apply = mkOption {
|
||||
type = with types; functionTo raw;
|
||||
default = requests: lib.mkMerge (requests ++ [ config.extra-config ]);
|
||||
};
|
||||
};
|
||||
config.resource-type = types.raw; # TODO: what's the type of a NixOS configuration?
|
||||
config.apply = requests: lib.mkMerge (requests ++ [ config.extra-config ]);
|
||||
};
|
||||
};
|
||||
resources.login-shell = {
|
||||
|
|
@ -82,23 +80,25 @@ in
|
|||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
apply = mkOption {
|
||||
type = with types; functionTo raw; # TODO: splice out the user type from NixOS
|
||||
default =
|
||||
requests:
|
||||
let
|
||||
# Filter out requests that need wheel if policy doesn't allow it
|
||||
validRequests = lib.filterAttrs (_name: req: !req.wheel || config.wheel) requests;
|
||||
in
|
||||
lib.optionalAttrs (validRequests != { }) {
|
||||
${config.username} = {
|
||||
isNormalUser = true;
|
||||
packages = with lib; concatMapAttrs (_name: request: attrValues request.packages) validRequests;
|
||||
extraGroups = lib.optional config.wheel "wheel";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
config.resource-type = types.raw; # TODO: splice out the user type from NixOS
|
||||
config.apply =
|
||||
requests:
|
||||
let
|
||||
# Filter out requests that need wheel if policy doesn't allow it
|
||||
validRequests = lib.filterAttrs (
|
||||
_name: req: !req.login-shell.wheel || config.wheel
|
||||
) requests.resources;
|
||||
in
|
||||
lib.optionalAttrs (validRequests != { }) {
|
||||
${config.username} = {
|
||||
isNormalUser = true;
|
||||
packages =
|
||||
with lib;
|
||||
attrValues (concatMapAttrs (_name: request: request.login-shell.packages) validRequests);
|
||||
extraGroups = lib.optional config.wheel "wheel";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
applications.hello =
|
||||
|
|
@ -108,39 +108,42 @@ in
|
|||
module =
|
||||
{ ... }:
|
||||
{
|
||||
enable = lib.mkEnableOption "Hello in the shell";
|
||||
options.enable = lib.mkEnableOption "Hello in the shell";
|
||||
};
|
||||
implementation =
|
||||
cfg:
|
||||
lib.optionalAttrs cfg.enable {
|
||||
dummy.login-shell.packages.hello = pkgs.hello;
|
||||
implementation = cfg: {
|
||||
input = cfg;
|
||||
output = lib.optionalAttrs cfg.enable {
|
||||
resources.hello.login-shell.packages.hello = pkgs.hello;
|
||||
};
|
||||
};
|
||||
};
|
||||
environments.single-nixos-vm =
|
||||
{ config, ... }:
|
||||
{
|
||||
resources.shell.login-shell.username = "operator";
|
||||
implementation =
|
||||
requests:
|
||||
{ providers, ... }:
|
||||
{
|
||||
providers = {
|
||||
inherit (inputs.nixops4.modules.nixops4Provider) local;
|
||||
resources.operator-environment.login-shell.username = "operator";
|
||||
implementation = requests: {
|
||||
input = requests;
|
||||
output =
|
||||
{ providers, ... }:
|
||||
{
|
||||
providers = {
|
||||
inherit (inputs.nixops4.modules.nixops4Provider) local;
|
||||
};
|
||||
resources.the-machine = {
|
||||
type = providers.local.exec;
|
||||
imports = [
|
||||
inputs.nixops4-nixos.modules.nixops4Resource.nixos
|
||||
];
|
||||
nixos.module =
|
||||
{ ... }:
|
||||
{
|
||||
users.users = config.resources.shell.login-shell.apply (
|
||||
lib.filterAttrs (_name: value: value ? login-shell) requests
|
||||
);
|
||||
};
|
||||
};
|
||||
};
|
||||
resources.the-machine = {
|
||||
type = providers.local.exec;
|
||||
imports = [
|
||||
inputs.nixops4-nixos.modules.nixops4Resource.nixos
|
||||
];
|
||||
nixos.module =
|
||||
{ ... }:
|
||||
{
|
||||
users.users = config.resources.shell.login-shell.apply (
|
||||
lib.filterAttrs (_name: value: value ? login-shell) requests
|
||||
);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
options = {
|
||||
|
|
@ -161,11 +164,42 @@ in
|
|||
}
|
||||
);
|
||||
in
|
||||
{
|
||||
inherit (fediversity) example-deployment;
|
||||
rec {
|
||||
number-of-resources = with lib; length (attrNames fediversity.resources);
|
||||
inherit (fediversity) example-configuration;
|
||||
hello-package-exists =
|
||||
(fediversity.applications.hello.resources example-configuration.applications.hello)
|
||||
.resources.hello.login-shell.packages ? hello;
|
||||
wheel-required =
|
||||
(fediversity.applications.hello.resources example-configuration.applications.hello)
|
||||
.resources.hello.login-shell.wheel;
|
||||
wheel-allowed =
|
||||
fediversity.environments.single-nixos-vm.resources.operator-environment.login-shell.wheel;
|
||||
operator-shell =
|
||||
let
|
||||
resources = fediversity.applications.hello.resources example-configuration.applications.hello;
|
||||
users = fediversity.environments.single-nixos-vm.resources.operator-environment.login-shell.apply resources;
|
||||
in
|
||||
{
|
||||
inherit (users.operator) isNormalUser;
|
||||
packages = with lib; map (p: "${p.pname}") users.operator.packages;
|
||||
extraGroups = users.operator.extraGroups;
|
||||
};
|
||||
};
|
||||
expected =
|
||||
{
|
||||
expected = {
|
||||
number-of-resources = 2;
|
||||
example-configuration = {
|
||||
enable = true;
|
||||
applications.hello.enable = true;
|
||||
};
|
||||
hello-package-exists = true;
|
||||
wheel-required = false;
|
||||
wheel-allowed = false;
|
||||
operator-shell = {
|
||||
isNormalUser = true;
|
||||
packages = [ "hello" ];
|
||||
extraGroups = [ ];
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,19 +17,28 @@ let
|
|||
;
|
||||
|
||||
functionType = import ./function.nix;
|
||||
application-resources = {
|
||||
application-resources = submodule {
|
||||
options.resources = mkOption {
|
||||
# TODO: maybe transpose, and group the resources by type instead
|
||||
type = attrsOf (
|
||||
attrTag (
|
||||
lib.mapAttrs' (name: resource: {
|
||||
${name} = mkOption { type = resource.request; };
|
||||
}) config.resources
|
||||
lib.mapAttrs (_name: resource: mkOption { type = submodule resource.request; }) config.resources
|
||||
)
|
||||
);
|
||||
};
|
||||
};
|
||||
nixops4Deployment = inputs.nixops4.modules.nixops4Deployment.default;
|
||||
nixops4Deployment = types.deferredModuleWith {
|
||||
staticModules = [
|
||||
inputs.nixops4.modules.nixops4Deployment.default
|
||||
|
||||
{
|
||||
_module.args = {
|
||||
resourceProviderSystem = builtins.currentSystem;
|
||||
resources = { };
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
in
|
||||
{
|
||||
options = {
|
||||
|
|
@ -53,12 +62,24 @@ in
|
|||
description = "Options for configuring the resource policy for the hosting provider, a description of how the resource is made available";
|
||||
type = deferredModuleWith {
|
||||
staticModules = [
|
||||
{
|
||||
(policy: {
|
||||
_class = "fediversity-resource-policy";
|
||||
# TODO(@fricklerhandwerk): not sure it can be made
|
||||
# sensible syntactically, but essentially we want to
|
||||
# ensure that `apply` is defined, but since its output
|
||||
# depends on the specific policy we also need to
|
||||
# determine that somehow.
|
||||
# hopefully this also helps with correct composition down the line.
|
||||
options.resource-type = mkOption {
|
||||
description = "The type of resource this policy configures";
|
||||
type = types.optionType;
|
||||
};
|
||||
# TODO(@fricklerhandwerk): do we need a function type here as well, or is it in the way?
|
||||
options.apply = mkOption {
|
||||
description = "Apply the policy to a request";
|
||||
type = with types; functionTo policy.config.resource-type;
|
||||
};
|
||||
}
|
||||
})
|
||||
];
|
||||
};
|
||||
};
|
||||
|
|
@ -96,7 +117,7 @@ in
|
|||
type = submodule functionType;
|
||||
readOnly = true;
|
||||
default = {
|
||||
input-type = application.config.module;
|
||||
input-type = submodule application.config.module;
|
||||
output-type = application-resources;
|
||||
};
|
||||
};
|
||||
|
|
@ -133,12 +154,12 @@ in
|
|||
readOnly = true;
|
||||
default = {
|
||||
input-type = application-resources;
|
||||
output-type = nixops4Deployment;
|
||||
output-type = types.raw;
|
||||
};
|
||||
};
|
||||
deployment = mkOption {
|
||||
description = "Generate a deployment from a configuration";
|
||||
type = functionTo (submodule environment.config.resource-mapping.output-type);
|
||||
type = functionTo environment.config.resource-mapping.output-type;
|
||||
readOnly = true;
|
||||
default =
|
||||
cfg:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
let
|
||||
inherit (lib) mkOption types;
|
||||
inherit (types)
|
||||
deferredModule
|
||||
submodule
|
||||
functionTo
|
||||
optionType
|
||||
|
|
@ -14,10 +13,10 @@ in
|
|||
{
|
||||
options = {
|
||||
input-type = mkOption {
|
||||
type = deferredModule;
|
||||
type = optionType;
|
||||
};
|
||||
output-type = mkOption {
|
||||
type = deferredModule;
|
||||
type = optionType;
|
||||
};
|
||||
function-type = mkOption {
|
||||
type = optionType;
|
||||
|
|
@ -26,10 +25,10 @@ in
|
|||
submodule (function: {
|
||||
options = {
|
||||
input = mkOption {
|
||||
type = submodule config.input-type;
|
||||
type = config.input-type;
|
||||
};
|
||||
output = mkOption {
|
||||
type = submodule config.output-type;
|
||||
type = config.output-type;
|
||||
};
|
||||
};
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue