forked from Fediversity/Fediversity
Compare commits
1 commit
data-model
...
main
Author | SHA1 | Date | |
---|---|---|---|
e5710979ba |
11 changed files with 36 additions and 482 deletions
|
@ -13,7 +13,7 @@ jobs:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Update pins
|
- name: Update pins
|
||||||
run: nix-shell --run "npins --verbose update"
|
run: nix-shell --run "npins update"
|
||||||
- name: Create PR
|
- name: Create PR
|
||||||
uses: https://github.com/KiaraGrouwstra/gitea-create-pull-request@f9f80aa5134bc5c03c38f5aaa95053492885b397
|
uses: https://github.com/KiaraGrouwstra/gitea-create-pull-request@f9f80aa5134bc5c03c38f5aaa95053492885b397
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -11,8 +11,7 @@ let
|
||||||
;
|
;
|
||||||
inherit (pkgs) lib;
|
inherit (pkgs) lib;
|
||||||
inherit (import sources.flake-inputs) import-flake;
|
inherit (import sources.flake-inputs) import-flake;
|
||||||
inputs = (import-flake { src = ./.; }).inputs;
|
inherit ((import-flake { src = ./.; }).inputs) nixops4;
|
||||||
inherit (inputs) nixops4;
|
|
||||||
panel = import ./panel { inherit sources system; };
|
panel = import ./panel { inherit sources system; };
|
||||||
pre-commit-check =
|
pre-commit-check =
|
||||||
(import "${git-hooks}/nix" {
|
(import "${git-hooks}/nix" {
|
||||||
|
@ -79,7 +78,6 @@ in
|
||||||
# re-export inputs so they can be overridden granularly
|
# re-export inputs so they can be overridden granularly
|
||||||
# (they can't be accessed from the outside any other way)
|
# (they can't be accessed from the outside any other way)
|
||||||
inherit
|
inherit
|
||||||
inputs
|
|
||||||
sources
|
sources
|
||||||
system
|
system
|
||||||
pkgs
|
pkgs
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
let
|
let
|
||||||
inherit (import ../default.nix { }) pkgs inputs;
|
inherit (import ../default.nix { }) pkgs inputs;
|
||||||
inherit (pkgs) lib;
|
inherit (pkgs) lib;
|
||||||
inherit (lib) mkOption types;
|
inherit (lib) mkOption;
|
||||||
eval =
|
eval =
|
||||||
module:
|
module:
|
||||||
(lib.evalModules {
|
(lib.evalModules {
|
||||||
|
@ -13,82 +13,17 @@ let
|
||||||
./data-model.nix
|
./data-model.nix
|
||||||
];
|
];
|
||||||
}).config;
|
}).config;
|
||||||
inherit (inputs.nixops4.lib) mkDeployment;
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
_class = "nix-unit";
|
_class = "nix-unit";
|
||||||
|
|
||||||
test-eval = {
|
test-eval = {
|
||||||
/**
|
|
||||||
This tests a very simple arrangement that features all ingredients of the Fediversity business logic:
|
|
||||||
application, resource, environment, deployment; and wires it all up in one end-to-end exercise.
|
|
||||||
- The dummy resource is a login shell made available for some user.
|
|
||||||
- The dummy application is `hello` that requires a shell to be deployed.
|
|
||||||
- The dummy environment is a single NixOS VM that hosts one login shell, for the operator.
|
|
||||||
- The dummy configuration enables the `hello` application.
|
|
||||||
This will produce a NixOps4 deployment for a NixOS VM with a login shell for the operator and `hello` available.
|
|
||||||
*/
|
|
||||||
expr =
|
expr =
|
||||||
let
|
let
|
||||||
fediversity = eval (
|
fediversity = eval (
|
||||||
{ config, options, ... }:
|
{ config, ... }:
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
resources.login-shell = {
|
|
||||||
description = "The operator needs to be able to log into the shell";
|
|
||||||
request =
|
|
||||||
{ ... }:
|
|
||||||
{
|
|
||||||
_class = "fediversity-resource-request";
|
|
||||||
options = {
|
|
||||||
wheel = mkOption {
|
|
||||||
description = "Whether the login user needs root permissions";
|
|
||||||
type = types.bool;
|
|
||||||
default = false;
|
|
||||||
};
|
|
||||||
packages = mkOption {
|
|
||||||
description = "Packages that need to be available in the user environment";
|
|
||||||
type = with types; attrsOf package;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
policy =
|
|
||||||
{ config, ... }:
|
|
||||||
{
|
|
||||||
_class = "fediversity-resource-policy";
|
|
||||||
options = {
|
|
||||||
username = mkOption {
|
|
||||||
description = "Username for the operator";
|
|
||||||
type = types.str; # TODO: use the proper constraints from NixOS
|
|
||||||
};
|
|
||||||
wheel = mkOption {
|
|
||||||
description = "Whether to allow login with root permissions";
|
|
||||||
type = types.bool;
|
|
||||||
default = false;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
config = {
|
|
||||||
resource-type = types.raw; # TODO: splice out the user type from NixOS
|
|
||||||
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 =
|
applications.hello =
|
||||||
{ ... }:
|
{ ... }:
|
||||||
{
|
{
|
||||||
|
@ -96,42 +31,15 @@ in
|
||||||
module =
|
module =
|
||||||
{ ... }:
|
{ ... }:
|
||||||
{
|
{
|
||||||
options.enable = lib.mkEnableOption "Hello in the shell";
|
options = {
|
||||||
};
|
enable = lib.mkEnableOption "Hello in the shell";
|
||||||
implementation = cfg: {
|
|
||||||
input = cfg;
|
|
||||||
output = lib.optionalAttrs cfg.enable {
|
|
||||||
resources.hello.login-shell.packages.hello = pkgs.hello;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
environments.single-nixos-vm =
|
|
||||||
{ config, ... }:
|
|
||||||
{
|
|
||||||
resources.operator-environment.login-shell.username = "operator";
|
|
||||||
implementation = requests: {
|
|
||||||
input = requests;
|
|
||||||
output.nixops4 =
|
|
||||||
{ 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
|
|
||||||
);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
implementation =
|
||||||
|
cfg:
|
||||||
|
lib.optionalAttrs cfg.enable {
|
||||||
|
dummy.login-shell.packages.hello = pkgs.hello;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
options = {
|
options = {
|
||||||
|
@ -143,64 +51,20 @@ in
|
||||||
applications.hello.enable = true;
|
applications.hello.enable = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
example-deployment = mkOption {
|
|
||||||
type = options.deployments.nestedType;
|
|
||||||
readOnly = true;
|
|
||||||
default = config.environments.single-nixos-vm.deployment config.example-configuration;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
resources = fediversity.applications.hello.resources fediversity.example-configuration.applications.hello;
|
|
||||||
hello-shell = resources.resources.hello.login-shell;
|
|
||||||
environment = fediversity.environments.single-nixos-vm.resources.operator-environment.login-shell;
|
|
||||||
result = mkDeployment {
|
|
||||||
modules = [
|
|
||||||
(fediversity.environments.single-nixos-vm.deployment fediversity.example-configuration)
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
number-of-resources = with lib; length (attrNames fediversity.resources);
|
inherit (fediversity)
|
||||||
inherit (fediversity) example-configuration;
|
example-configuration
|
||||||
hello-package-exists = hello-shell.packages ? hello;
|
;
|
||||||
wheel-required = hello-shell.wheel;
|
|
||||||
wheel-allowed = environment.wheel;
|
|
||||||
operator-shell =
|
|
||||||
let
|
|
||||||
operator = (environment.apply resources).operator;
|
|
||||||
in
|
|
||||||
{
|
|
||||||
inherit (operator) isNormalUser;
|
|
||||||
packages = map (p: "${p.pname}") operator.packages;
|
|
||||||
extraGroups = operator.extraGroups;
|
|
||||||
};
|
|
||||||
deployment = {
|
|
||||||
inherit (result) _type;
|
|
||||||
deploymentFunction = lib.isFunction result.deploymentFunction;
|
|
||||||
getProviders = lib.isFunction result.getProviders;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
expected = {
|
expected = {
|
||||||
number-of-resources = 1;
|
|
||||||
example-configuration = {
|
example-configuration = {
|
||||||
enable = true;
|
enable = true;
|
||||||
applications.hello.enable = true;
|
applications.hello.enable = true;
|
||||||
};
|
};
|
||||||
hello-package-exists = true;
|
|
||||||
wheel-required = false;
|
|
||||||
wheel-allowed = false;
|
|
||||||
operator-shell = {
|
|
||||||
isNormalUser = true;
|
|
||||||
packages = [ "hello" ];
|
|
||||||
extraGroups = [ ];
|
|
||||||
};
|
|
||||||
deployment = {
|
|
||||||
_type = "nixops4Deployment";
|
|
||||||
deploymentFunction = true;
|
|
||||||
getProviders = true;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
{
|
{
|
||||||
lib,
|
lib,
|
||||||
config,
|
config,
|
||||||
inputs,
|
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
|
@ -16,83 +15,19 @@ let
|
||||||
;
|
;
|
||||||
|
|
||||||
functionType = import ./function.nix;
|
functionType = import ./function.nix;
|
||||||
application-resources = submodule {
|
application-resources = {
|
||||||
options.resources = mkOption {
|
options.resources = mkOption {
|
||||||
# TODO: maybe transpose, and group the resources by type instead
|
# TODO: maybe transpose, and group the resources by type instead
|
||||||
type = attrsOf (
|
type = attrsOf (
|
||||||
attrTag (
|
attrTag (lib.mapAttrs (_name: resource: mkOption { type = resource.request; }) config.resources)
|
||||||
lib.mapAttrs (_name: resource: mkOption { type = submodule resource.request; }) config.resources
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
nixops4Deployment = types.deferredModuleWith {
|
|
||||||
staticModules = [
|
|
||||||
inputs.nixops4.modules.nixops4Deployment.default
|
|
||||||
|
|
||||||
{
|
|
||||||
_class = "nixops4Deployment";
|
|
||||||
_module.args = {
|
|
||||||
resourceProviderSystem = builtins.currentSystem;
|
|
||||||
resources = { };
|
|
||||||
};
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
deployment = attrTag {
|
|
||||||
nixops4 = mkOption {
|
|
||||||
description = "A NixOps4 NixOS deployment. For an example, see https://github.com/nixops4/nixops4-nixos/blob/main/example/deployment.nix.";
|
|
||||||
type = nixops4Deployment;
|
|
||||||
};
|
|
||||||
tf-ssh = mkOption {
|
|
||||||
description = "A Terraform deployment by SSH to update a single existing NixOS host";
|
|
||||||
type = types.attrset;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
_class = "nixops4Deployment";
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
resources = mkOption {
|
|
||||||
description = "Collection of deployment resources that can be required by applications and policed by hosting providers";
|
|
||||||
type = attrsOf (
|
|
||||||
submodule (
|
|
||||||
{ ... }:
|
|
||||||
{
|
|
||||||
_class = "fediversity-resource";
|
|
||||||
options = {
|
|
||||||
description = mkOption {
|
|
||||||
description = "Description of the resource to help application module authors and hosting providers to work with it";
|
|
||||||
type = types.str;
|
|
||||||
};
|
|
||||||
request = mkOption {
|
|
||||||
description = "Options for declaring resource requirements by an application, a description of how the resource is consumed or accessed";
|
|
||||||
type = deferredModuleWith { staticModules = [ { _class = "fediversity-resource-request"; } ]; };
|
|
||||||
};
|
|
||||||
policy = mkOption {
|
|
||||||
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";
|
|
||||||
options.resource-type = mkOption {
|
|
||||||
description = "The type of resource this policy configures";
|
|
||||||
type = types.optionType;
|
|
||||||
};
|
|
||||||
# TODO(@fricklerhandwerk): we may want to make the function type explict here: `request -> resource-type`
|
|
||||||
# and then also rename this to be consistent with the application's resource mapping
|
|
||||||
options.apply = mkOption {
|
|
||||||
description = "Apply the policy to a request";
|
|
||||||
type = functionTo policy.config.resource-type;
|
|
||||||
};
|
|
||||||
})
|
|
||||||
];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
applications = mkOption {
|
applications = mkOption {
|
||||||
description = "Collection of Fediversity applications";
|
description = "Collection of Fediversity applications";
|
||||||
type = attrsOf (
|
type = attrsOf (
|
||||||
|
@ -117,13 +52,12 @@ in
|
||||||
readOnly = true;
|
readOnly = true;
|
||||||
default = input: (application.config.implementation input).output;
|
default = input: (application.config.implementation input).output;
|
||||||
};
|
};
|
||||||
# TODO(@fricklerhandwerk): this needs a better name, it's just the type
|
|
||||||
config-mapping = mkOption {
|
config-mapping = mkOption {
|
||||||
description = "Function type for the mapping from application configuration to required resources";
|
description = "Function type for the mapping from application configuration to required resources";
|
||||||
type = submodule functionType;
|
type = submodule functionType;
|
||||||
readOnly = true;
|
readOnly = true;
|
||||||
default = {
|
default = {
|
||||||
input-type = submodule application.config.module;
|
input-type = application.config.module;
|
||||||
output-type = application-resources;
|
output-type = application-resources;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -131,60 +65,6 @@ in
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
environments = mkOption {
|
|
||||||
description = "Run-time environments for Fediversity applications to be deployed to";
|
|
||||||
type = attrsOf (
|
|
||||||
submodule (environment: {
|
|
||||||
_class = "fediversity-environment";
|
|
||||||
options = {
|
|
||||||
resources = mkOption {
|
|
||||||
description = ''
|
|
||||||
Resources made available by the hosting provider, and their policies.
|
|
||||||
|
|
||||||
Setting this is optional, but provides a place to declare that information for programmatic use in the resource mapping.
|
|
||||||
'';
|
|
||||||
# TODO: maybe transpose, and group the resources by type instead
|
|
||||||
type = attrsOf (
|
|
||||||
attrTag (
|
|
||||||
lib.mapAttrs (_name: resource: mkOption { type = submodule resource.policy; }) config.resources
|
|
||||||
)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
implementation = mkOption {
|
|
||||||
description = "Mapping of resources required by applications to available resources; the result can be deployed";
|
|
||||||
type = environment.config.resource-mapping.function-type;
|
|
||||||
};
|
|
||||||
resource-mapping = mkOption {
|
|
||||||
description = "Function type for the mapping from resources to a deployment";
|
|
||||||
type = submodule functionType;
|
|
||||||
readOnly = true;
|
|
||||||
default = {
|
|
||||||
input-type = application-resources;
|
|
||||||
output-type = deployment;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
# TODO(@fricklerhandwerk): maybe this should be a separate thing such as `fediversity-setup`,
|
|
||||||
# which makes explicit which applications and environments are available.
|
|
||||||
# then the deployments can simply be the result of the function application baked into this module.
|
|
||||||
deployment = mkOption {
|
|
||||||
description = "Generate a deployment from a configuration, by applying an environment's resource policies to the applications' resource mappings";
|
|
||||||
type = functionTo (environment.config.resource-mapping.output-type);
|
|
||||||
readOnly = true;
|
|
||||||
default =
|
|
||||||
cfg:
|
|
||||||
# TODO: check cfg.enable.true
|
|
||||||
let
|
|
||||||
required-resources = lib.mapAttrs (
|
|
||||||
name: application-settings: config.applications.${name}.resources application-settings
|
|
||||||
) cfg.applications;
|
|
||||||
in
|
|
||||||
(environment.config.implementation required-resources).output;
|
|
||||||
|
|
||||||
};
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
|
||||||
configuration = mkOption {
|
configuration = mkOption {
|
||||||
description = "Configuration type declaring options to be set by operators";
|
description = "Configuration type declaring options to be set by operators";
|
||||||
type = optionType;
|
type = optionType;
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
let
|
let
|
||||||
inherit (lib) mkOption types;
|
inherit (lib) mkOption types;
|
||||||
inherit (types)
|
inherit (types)
|
||||||
|
deferredModule
|
||||||
submodule
|
submodule
|
||||||
functionTo
|
functionTo
|
||||||
optionType
|
optionType
|
||||||
|
@ -13,10 +14,10 @@ in
|
||||||
{
|
{
|
||||||
options = {
|
options = {
|
||||||
input-type = mkOption {
|
input-type = mkOption {
|
||||||
type = optionType;
|
type = deferredModule;
|
||||||
};
|
};
|
||||||
output-type = mkOption {
|
output-type = mkOption {
|
||||||
type = optionType;
|
type = deferredModule;
|
||||||
};
|
};
|
||||||
function-type = mkOption {
|
function-type = mkOption {
|
||||||
type = optionType;
|
type = optionType;
|
||||||
|
@ -24,10 +25,10 @@ in
|
||||||
default = functionTo (submodule {
|
default = functionTo (submodule {
|
||||||
options = {
|
options = {
|
||||||
input = mkOption {
|
input = mkOption {
|
||||||
type = config.input-type;
|
type = submodule config.input-type;
|
||||||
};
|
};
|
||||||
output = mkOption {
|
output = mkOption {
|
||||||
type = config.output-type;
|
type = submodule config.output-type;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
let
|
|
||||||
vm_domain = "abundos.eu";
|
|
||||||
in
|
|
||||||
{
|
|
||||||
module."nixos" =
|
|
||||||
builtins.mapAttrs
|
|
||||||
(service: hostname: {
|
|
||||||
source = "../sync-nix";
|
|
||||||
inherit vm_domain hostname;
|
|
||||||
config_tf = {
|
|
||||||
terraform = {
|
|
||||||
inherit hostname;
|
|
||||||
domain = vm_domain;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
config_nix = ''
|
|
||||||
{
|
|
||||||
imports = [
|
|
||||||
# shared NixOS config
|
|
||||||
$${path.root}/../common/shared.nix
|
|
||||||
# FIXME: separate template options by service
|
|
||||||
$${path.root}/options.nix
|
|
||||||
# for service `forgejo` import `forgejo.nix`
|
|
||||||
$${path.root}/../../machines/dev/${hostname}/${service}.nix
|
|
||||||
# FIXME: get VM details from TF
|
|
||||||
$${path.root}/../../machines/dev/${hostname}
|
|
||||||
];
|
|
||||||
}
|
|
||||||
'';
|
|
||||||
})
|
|
||||||
{
|
|
||||||
# wiki = "vm02187" # does not resolve
|
|
||||||
# forgejo = "vm02116" # does not resolve
|
|
||||||
# TODO: move these to a separate `host` dir
|
|
||||||
dns = "fedi200";
|
|
||||||
# fedipanel = "fedi201";
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -23,17 +23,21 @@ let
|
||||||
makeResourceModule =
|
makeResourceModule =
|
||||||
{ vmName, isTestVm }:
|
{ vmName, isTestVm }:
|
||||||
{
|
{
|
||||||
|
# TODO(@fricklerhandwerk): this is terrible but IMO we should just ditch flake-parts and have our own data model for how the project is organised internally
|
||||||
|
_module.args = {
|
||||||
|
inherit
|
||||||
|
inputs
|
||||||
|
keys
|
||||||
|
secrets
|
||||||
|
;
|
||||||
|
};
|
||||||
|
|
||||||
nixos.module.imports = [
|
nixos.module.imports = [
|
||||||
./common/proxmox-qemu-vm.nix
|
./common/proxmox-qemu-vm.nix
|
||||||
];
|
];
|
||||||
|
|
||||||
nixos.specialArgs = {
|
nixos.specialArgs = {
|
||||||
inherit
|
inherit sources;
|
||||||
sources
|
|
||||||
inputs
|
|
||||||
keys
|
|
||||||
secrets
|
|
||||||
;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
imports =
|
imports =
|
||||||
|
@ -75,13 +79,7 @@ let
|
||||||
# TODO(@fricklerhandwerk): we may want to pass through all of `specialArgs`
|
# TODO(@fricklerhandwerk): we may want to pass through all of `specialArgs`
|
||||||
# once we're sure it's sane. leaving it here for better control during refactoring.
|
# once we're sure it's sane. leaving it here for better control during refactoring.
|
||||||
specialArgs = {
|
specialArgs = {
|
||||||
inherit
|
inherit sources;
|
||||||
sources
|
|
||||||
inputs
|
|
||||||
keys
|
|
||||||
secrets
|
|
||||||
|
|
||||||
;
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,91 +0,0 @@
|
||||||
locals {
|
|
||||||
system = "x86_64-linux"
|
|
||||||
# dependency paths pre-calculated from npins
|
|
||||||
pins = jsondecode(file("${path.module}/.npins.json"))
|
|
||||||
# nix path: expose pins, use nixpkgs in flake commands (`nix run`)
|
|
||||||
nix_path = "${join(":", [for name, dir in local.pins : "${name}=${dir}"])}:flake=${local.pins["nixpkgs"]}:flake"
|
|
||||||
}
|
|
||||||
|
|
||||||
# hash of our code directory, used to trigger re-deploy
|
|
||||||
# FIXME calculate separately to reduce false positives
|
|
||||||
data "external" "hash" {
|
|
||||||
program = ["sh", "-c", "echo \"{\\\"hash\\\":\\\"$(nix-hash ..)\\\"}\""]
|
|
||||||
}
|
|
||||||
|
|
||||||
# TF resource to build and deploy NixOS instances.
|
|
||||||
resource "terraform_data" "nixos" {
|
|
||||||
|
|
||||||
# trigger rebuild/deploy if (FIXME?) any potentially used config/code changed,
|
|
||||||
# preventing these (20+s, build being bottleneck) when nothing changed.
|
|
||||||
# terraform-nixos separates these to only deploy if instantiate changed,
|
|
||||||
# yet building even then - which may be not as bad using deploy on remote.
|
|
||||||
# having build/deploy one resource reflects wanting to prevent no-op rebuilds
|
|
||||||
# over preventing (with less false positives) no-op deployments,
|
|
||||||
# as i could not find a way to do prevent no-op rebuilds without merging them:
|
|
||||||
# - generic resources cannot have outputs, while we want info from the instantiation (unless built on host?).
|
|
||||||
# - `data` always runs, which is slow for deploy and especially build.
|
|
||||||
triggers_replace = [
|
|
||||||
data.external.hash.result,
|
|
||||||
var.hostname,
|
|
||||||
var.config_nix,
|
|
||||||
var.config_tf,
|
|
||||||
]
|
|
||||||
|
|
||||||
provisioner "local-exec" {
|
|
||||||
# directory to run the script from. we use the TF project root dir,
|
|
||||||
# here as a path relative from where TF is run from,
|
|
||||||
# matching calling modules' expectations on config_nix locations.
|
|
||||||
# note that absolute paths can cause false positives in triggers,
|
|
||||||
# so are generally discouraged in TF.
|
|
||||||
working_dir = path.root
|
|
||||||
environment = {
|
|
||||||
# nix path used on build, lets us refer to e.g. nixpkgs like `<nixpkgs>`
|
|
||||||
NIX_PATH = local.nix_path
|
|
||||||
}
|
|
||||||
# TODO: refactor back to command="ignoreme" interpreter=concat([]) to protect sensitive data from error logs?
|
|
||||||
# TODO: build on target?
|
|
||||||
command = <<-EOF
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
# INSTANTIATE
|
|
||||||
command=(
|
|
||||||
nix-instantiate
|
|
||||||
--expr
|
|
||||||
'import ./nixos.nix {
|
|
||||||
system = "${local.system}";
|
|
||||||
configuration =
|
|
||||||
${var.config_nix} //
|
|
||||||
builtins.fromJSON "${replace(jsonencode(var.config_tf), "\"", "\\\"")}" //
|
|
||||||
{
|
|
||||||
nix.nixPath = [ "${local.nix_path}" ];
|
|
||||||
};
|
|
||||||
}'
|
|
||||||
)
|
|
||||||
# instantiate the config in /nix/store
|
|
||||||
"$${command[@]}" -A out_path
|
|
||||||
# get the other info
|
|
||||||
json="$("$${command[@]}" --eval --strict --json)"
|
|
||||||
|
|
||||||
# DEPLOY
|
|
||||||
declare substituters trusted_public_keys drv_path
|
|
||||||
# set our variables using the json object
|
|
||||||
eval "export $(echo $json | jaq -r 'to_entries | map("\(.key)=\(.value)") | @sh')"
|
|
||||||
host="root@${var.hostname}.${var.vm_domain}" # FIXME: #24
|
|
||||||
buildArgs=(
|
|
||||||
--option extra-binary-caches https://cache.nixos.org/
|
|
||||||
--option substituters $substituters
|
|
||||||
--option trusted-public-keys $trusted_public_keys
|
|
||||||
)
|
|
||||||
sshOpts=(
|
|
||||||
-o BatchMode=yes
|
|
||||||
-o StrictHostKeyChecking=no
|
|
||||||
)
|
|
||||||
# get the realized derivation to deploy
|
|
||||||
outPath=$(nix-store --realize "$drv_path" "$${buildArgs[@]}")
|
|
||||||
# deploy the config by nix-copy-closure
|
|
||||||
NIX_SSHOPTS="$${sshOpts[*]}" nix-copy-closure --to "$host" "$outPath" --gzip --use-substitutes
|
|
||||||
# switch the remote host to the config
|
|
||||||
ssh "$${sshOpts[@]}" "$host" "nix-env --profile /nix/var/nix/profiles/system --set $outPath; $outPath/bin/switch-to-configuration switch"
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
{
|
|
||||||
configuration,
|
|
||||||
system ? builtins.currentSystem,
|
|
||||||
}:
|
|
||||||
let
|
|
||||||
sources = import ../../npins;
|
|
||||||
os = import "${sources.nixpkgs}/nixos" { inherit system configuration; };
|
|
||||||
in
|
|
||||||
{
|
|
||||||
substituters = builtins.concatStringsSep " " os.config.nix.settings.substituters;
|
|
||||||
trusted_public_keys = builtins.concatStringsSep " " os.config.nix.settings.trusted-public-keys;
|
|
||||||
drv_path = os.config.system.build.toplevel.drvPath;
|
|
||||||
out_path = os.config.system.build.toplevel;
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
# TODO: generate nix module from `variables.tf`
|
|
||||||
# - TF -> JSON schema: https://melvinkoh.me/parsing-terraform-for-forms-clr4zq4tu000309juab3r1lf7
|
|
||||||
# - python313Packages.python-hcl2: hcl2tojson variables.tf
|
|
||||||
# - JSON schema -> nix
|
|
||||||
{ lib, ... }:
|
|
||||||
let
|
|
||||||
inherit (lib) mkOption types;
|
|
||||||
inherit (types) string attrsOf any;
|
|
||||||
in
|
|
||||||
{
|
|
||||||
options = {
|
|
||||||
vm_domain = mkOption {
|
|
||||||
type = string;
|
|
||||||
};
|
|
||||||
hostname = mkOption {
|
|
||||||
type = string;
|
|
||||||
};
|
|
||||||
config_nix = mkOption {
|
|
||||||
type = string;
|
|
||||||
default = { };
|
|
||||||
};
|
|
||||||
config_tf = mkOption {
|
|
||||||
type = attrsOf any;
|
|
||||||
default = { };
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
variable "vm_domain" {
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "hostname" {
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "config_nix" {
|
|
||||||
type = string
|
|
||||||
default = "{}"
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "config_tf" {
|
|
||||||
type = map(any)
|
|
||||||
default = {}
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue