forked from Fediversity/Fediversity
this shows a proof of concept for generating Django forms from NixOS modules
note that the form behavior is still rather clumsy and doesn't exactly map to the module semantics:
- since forms can only be sent wholesale, empty form fields will show up as empty strings
and break validation without additional cleanup (not done here)
- it's not possible to faithfully translate `type = submodule { /* ... */}; default = {};`, since the default
is translated to an empty dict `{}`. this is because the JSON schema converter does not preserve type information.
this can be added by making it use `$defs` [1], but that would likely amount to half a rewrite
- there's a glitch in enum default values that needs to be fixed in `datamodel-code-generator` [0]
[0]: dd44480359/src/datamodel_code_generator/parser/base.py (L1015)
[1]: https://json-schema.org/understanding-json-schema/structuring#defs
a generated file will be placed into the source (by the development shell and the package respectively)
that declares Pydantic types from which to render the form. it looks something like this:
```python
from __future__ import annotations
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Extra, Field
from drf_pydantic import BaseModel
class Domain(Enum):
fediversity_net = 'fediversity.net'
# ...
class Model(BaseModel):
class Config:
extra = Extra.forbid
domain: Optional[Domain] = Field(
'fediversity.net',
description='Apex domain under which the services will be deployed.\n',
)
# ...
```
102 lines
2.7 KiB
Nix
102 lines
2.7 KiB
Nix
/**
|
|
Deployment options as to be presented in the front end.
|
|
|
|
These are converted to JSON schema in order to generate front-end forms etc.
|
|
For this to work, options must not have types `functionTo` or `package`, and must not access `config` for their default values.
|
|
|
|
The options are written in a cumbersome way because the JSON schema converter can't evaluate a submodule option's default value, which thus all must be set to `null`.
|
|
This can be fixed if we made the converter aware of [`$defs`], but that would likely amount to half a rewrite.
|
|
|
|
[`$defs`]: https://json-schema.org/understanding-json-schema/structuring#defs
|
|
*/
|
|
{
|
|
lib,
|
|
...
|
|
}:
|
|
let
|
|
inherit (lib) types mkOption;
|
|
in
|
|
{
|
|
options = {
|
|
enable = lib.mkEnableOption "Fediversity configuration";
|
|
domain = mkOption {
|
|
type =
|
|
with types;
|
|
enum [
|
|
"fediversity.net"
|
|
];
|
|
description = ''
|
|
Apex domain under which the services will be deployed.
|
|
'';
|
|
default = "fediversity.net";
|
|
};
|
|
pixelfed = mkOption {
|
|
description = ''
|
|
Configuration for the Pixelfed service
|
|
'';
|
|
type =
|
|
with types;
|
|
nullOr (submodule {
|
|
options = {
|
|
enable = lib.mkEnableOption "Pixelfed";
|
|
};
|
|
});
|
|
default = null;
|
|
};
|
|
peertube = mkOption {
|
|
description = ''
|
|
Configuration for the PeerTube service
|
|
'';
|
|
type =
|
|
with types;
|
|
nullOr (submodule {
|
|
options = {
|
|
enable = lib.mkEnableOption "Peertube";
|
|
};
|
|
});
|
|
default = null;
|
|
};
|
|
mastodon = mkOption {
|
|
description = ''
|
|
Configuration for the Mastodon service
|
|
'';
|
|
type =
|
|
with types;
|
|
nullOr (submodule {
|
|
options = {
|
|
enable = lib.mkEnableOption "Mastodon";
|
|
};
|
|
});
|
|
default = null;
|
|
};
|
|
initialUser = mkOption {
|
|
description = ''
|
|
Some services require an initial user to access them.
|
|
This option sets the credentials for such an initial user.
|
|
'';
|
|
type =
|
|
with types;
|
|
nullOr (submodule {
|
|
options = {
|
|
displayName = mkOption {
|
|
type = types.str;
|
|
description = "Display name of the user";
|
|
};
|
|
username = mkOption {
|
|
type = types.str;
|
|
description = "Username for login";
|
|
};
|
|
email = mkOption {
|
|
type = types.str;
|
|
description = "User's email address";
|
|
};
|
|
password = mkOption {
|
|
type = types.str;
|
|
description = "Password for login";
|
|
};
|
|
};
|
|
});
|
|
default = null;
|
|
};
|
|
};
|
|
}
|