Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed models for projects #507

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ rec {
};
};

project-models = import ./projects/models.nix { inherit lib pkgs sources; };

# TODO: find a better place for this
metrics = with lib; {
projects = attrNames raw-projects;
Expand Down
19 changes: 18 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
inputs.sops-nix.url = "github:Mic92/sops-nix";
inputs.buildbot-nix.inputs.nixpkgs.follows = "nixpkgs";
inputs.buildbot-nix.url = "github:nix-community/buildbot-nix";
inputs.yants.url = "git+https://code.tvl.fyi/depot.git:/nix/yants.git";
inputs.yants.flake = false;

# See <https://github.com/ngi-nix/ngipkgs/issues/24> for plans to support Darwin.
inputs.systems.url = "github:nix-systems/default-linux";
Expand Down
2 changes: 2 additions & 0 deletions projects/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ let
allowedFiles = [
"README.md"
"default.nix"
"models.nix"
];
in
# TODO: use fileset and filter for `gitTracked` files
concatMapAttrs names (readDir baseDirectory);
in
mapAttrs (name: directory: import directory { inherit lib pkgs sources; }) projectDirectories
128 changes: 128 additions & 0 deletions projects/models.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
lib,
pkgs,
sources,
}:
let
yants = import sources.yants { };

inherit (yants)
string
list
option
attrs
enum
either
struct
drv
path
restrict
;

programType = struct "program" {
name = string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = string;
name = option string;

I don't see the need for a human-readable name yet, so we could leave that optional.

source = drv;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided programs should also be NixOS modules (programs.<symbolic application name>), so the actual sources would be an implementation detail we later think about whether and how expose it in the UI. So arguably that could just as well be

Suggested change
source = drv;
module = defun [attrs attrs];

documentation = optionalStruct {
build = option string;
tests = option string;
};
examples = nonEmtpyAttrs (option exampleType);
};

serviceType = struct "service" {
name = string;
documentation = optionalStruct {
config = option string;
};
examples = nonEmtpyAttrs (option exampleType);
};

exampleType = struct "example" {
description = option string; # TODO: should this be non-optional?
path = either string path;
documentation = option string;
tests = nonEmtpyAttrs drv;
};

optionalStruct = attrs: option (struct attrs);
nonEmtpyAttrs = t: restrict "non-empty-attrs" (a: a != { }) (attrs t);
in
rec {
project = struct {
name = string;
metadata = optionalStruct {
summary = option string;
subgrants = list string;
};
nixos = struct "nixos" {
examples = option (attrs exampleType);
tests = option (attrs (option drv));
modules = struct "modules" {
programs = option (attrs (option programType));
services = option (attrs (option serviceType));
};
};
};

example = project {
name = "foobar";
nixos = rec {
examples = {
foobar-cli = {
description = ''
This is how you can run `foobar` in the terminal.
'';
path = "";
documentation = "https://foo.bar/docs";
tests = {
# Each example must have at least one test.
# If the line below is commented out, an error will be raised.
inherit (tests) foobar-cli;
};
};
};
tests = {
# Set to `null`: needed, but not available
basic = null;

# Needs to be a derivation. Error raised otherwise.
#simple = "This will fail.";

foobar-cli = derivation {
name = "myname";
builder = "mybuilder";
system = "mysystem";
};
};
modules = {
# Attributes not defined in the data structure are not allowed.
# Uncommenting the line below will raise an error.
#hello = { };

programs = {
# Set to `null`: needed, but not available
foobar = null;

foobar-cli = {
name = "foobar-cli";
source = derivation {
name = "foobar-cli-src";
builder = "mybuilder";
system = "mysystem";
};
# Each program must have at least one example.
# Examples can be null to indicate that they're needed.
examples = {
inherit (examples) foobar-cli;

# needed, not available
foobar-tui = null;
};
};

# Not set: not needed
};
};
};
};
}