new mode: programs_and_clan

This commit is contained in:
DavHau 2024-04-29 12:35:00 +02:00
parent 327d166c6f
commit be3760afc2
5 changed files with 80 additions and 13 deletions

12
nix/clean-options-clan.jq Normal file
View File

@ -0,0 +1,12 @@
to_entries
| map(
select(.key | test(".*\\.enable$") | not)
| select(.value.readOnly == false)
| .value.option = .key
| del(.value.loc)
| del(.value.readOnly)
| .value.default = .value.default.text
| .value.example = .value.example.text
| del(..|select(. == null))
)
| from_entries

View File

@ -18,7 +18,7 @@ let
docsJson = docsOut + "/share/doc/nixos/options.json";
docsCleaned = pkgs.runCommand "options-cleaned.json" { } ''
cat ${docsJson} | ${pkgs.jq}/bin/jq "$(cat ${./clean-options.jq})" > $out
cat ${docsJson} | ${pkgs.jq}/bin/jq "$(cat ${./clean-options-clan.jq})" > $out
'';
in
# docsOut

View File

@ -1,9 +1,11 @@
from . import nixos_and_clan_old
# from . import nixos_and_clan_old
from . import nixos_and_clan
from . import programs_and_clan
from . import programs
prompts = {
"nixos_and_clan_old": nixos_and_clan_old.system_msg,
# "nixos_and_clan_old": nixos_and_clan_old.system_msg,
"nixos_and_clan": nixos_and_clan.system_msg,
"programs_and_clan": programs_and_clan.system_msg,
"programs": programs.system_msg,
}

View File

@ -0,0 +1,45 @@
# !/usr/bin/env python3
import os
from pathlib import Path
_here = Path(__file__).parent
_clan_docs = Path(os.environ["options_clan"]).read_text()
system_msg = f"""
# Task
{(_here / "static" / "task.md").read_text()}
# Format
{(_here / "static" / "format.md").read_text()}
# Template
{(_here / "static" / "template.md").read_text()}
# Restriction
Each user requirement should either be implemented:
- by adding one or more program to `environment.systemPackages`
- or by using a clan module
The use of clan modules should be preferred over adding programs.
Never import or configure any upstream nixos modules
## Programs
To install a program, add it to the `environment.systemPackages` list.
## Clan modules
Importing a clan module is sufficient to enable it.
Do not set enable options for clan modules.
For example, to enable the `git` module, add `clan-core.clanModules.git` to the imports but do not set `clan.git.enable = true`.
Only set mandatory options.
Don't set options that have a default
Only use options declared by the clan modules documentation (provided below).
Whenever you use such an option prefixed with `clan.`, like for example `clan.something` you also need to add a module named `something` to the imports like this:
```
imports = [
clan-core.clanModules.something
]
```
Here the documentation of the clan options as JSON:
{_clan_docs}
"""

View File

@ -16,9 +16,9 @@ _here = Path(__file__).parent
_base_config = (_here.parent / "prompts" / "static" / "base-config.nix").read_text()
def config_from_prompt(mode, prompt, conf_dir=Path("/tmp/clana")) -> str:
def config_from_prompt(system, prompt, conf_dir=Path("/tmp/clana")) -> str:
messages = [
{"role": "system", "content": mode},
{"role": "system", "content": system},
{"role": "user", "content": prompt},
]
return config_from_messages(messages, conf_dir=conf_dir)
@ -42,9 +42,9 @@ def config_from_messages(messages, conf_dir=Path("/tmp/clana")) -> str:
return config
def start_vm_from_prompt(mode, prompt, show=False):
def start_vm_from_prompt(system, prompt, show=False):
messages = [
{"role": "system", "content": mode},
{"role": "system", "content": system},
{"role": "user", "content": prompt},
]
conf_dir = Path("/tmp/clana")
@ -93,7 +93,7 @@ def start_vm_from_prompt(mode, prompt, show=False):
if "error:" in e.cmd.stderr \
else e.cmd.stderr
messages += [
{"role": "assistant", "content": config_orig},
{"role": "assistant", "content": config},
{
"role": "system",
"content": f"There was a problem that needs to be fixed:\n{error}",
@ -115,17 +115,25 @@ def main():
help="Which system message to use for generating the VM",
)
parser.add_argument(
"--show-only", "-s",
"--show-config", "-s",
action="store_true",
help="Print the generated configuration without launching the VM",
)
parser.add_argument(
"--show-system",
action="store_true",
help="Print the system message",
)
args = parser.parse_args()
mode = prompts.prompts[args.mode]
if args.show_only:
config = config_from_prompt(mode=mode, prompt=args.prompt)
system = prompts.prompts[args.mode]
if args.show_system:
print(system)
exit()
if args.show_config:
config = config_from_prompt(system=system, prompt=args.prompt)
print(config)
exit()
start_vm_from_prompt(mode=mode, prompt=args.prompt, show=True)
start_vm_from_prompt(system=system, prompt=args.prompt, show=True)
if __name__ == "__main__":
main()