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

profiles: simplify profiles to suites #139

Merged
merged 1 commit into from
Feb 25, 2021
Merged
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: 1 addition & 1 deletion hosts/NixOS.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ suites, ... }:
{
### root password is empty by default ###
imports = suites.core;
imports = suites.base;

boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
Expand Down
2 changes: 1 addition & 1 deletion hosts/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let

modules =
let
core = import ../profiles/core;
core = ../profiles/core;

modOverrides = { config, overrideModulesPath, ... }:
let
Expand Down
18 changes: 10 additions & 8 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let
pathExists filter;

inherit (nixos.lib) fold filterAttrs hasSuffix mapAttrs' nameValuePair removeSuffix
recursiveUpdate genAttrs nixosSystem mkForce;
recursiveUpdate genAttrs nixosSystem mkForce optionalAttrs;

# mapFilterAttrs ::
# (name -> value -> bool )
Expand Down Expand Up @@ -58,35 +58,37 @@ let
});

/**
Synopsis: importDefaults _path_
Synopsis: mkProfileAttrs _path_

Recursively import the subdirs of _path_ containing a default.nix.

Example:
let profiles = importDefaults ./profiles; in
let profiles = mkProfileAttrs ./profiles; in
assert profiles ? core.default; 0
**/
importDefaults = dir:
mkProfileAttrs = dir:
let
imports =
let
files = readDir dir;

p = n: v:
v == "directory"
&& pathExists "${dir}/${n}/default.nix";
&& n != "profiles";
in
filterAttrs p files;

f = n: _:
{ default = import "${dir}/${n}/default.nix"; }
// importDefaults "${dir}/${n}";
optionalAttrs
(pathExists "${dir}/${n}/default.nix")
{ default = "${dir}/${n}"; }
// mkProfileAttrs "${dir}/${n}";
in
mapAttrs f imports;

in
{
inherit importDefaults mapFilterAttrs genAttrs' pkgImport
inherit mkProfileAttrs mapFilterAttrs genAttrs' pkgImport
pathsToImportedAttrs mkNodes;

overlays = pathsToImportedAttrs overlayPaths;
Expand Down
35 changes: 28 additions & 7 deletions profiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ window manager. If you need some concrete examples, just checkout the
community [branch](https://github.com/divnix/devos/tree/community/profiles).

## Constraints
For the sake of consistency, there are a few minor constraints. First of all, a
profile should always be defined in a `default.nix`, and it should always be a
a function taking a single attribute set as an argument, and returning a NixOS
module which does not define any new module options. If you need to make new
module option declarations, just use [modules](../modules).
For the sake of consistency, a profile should always be defined in a
_default.nix_ containing a valid [nixos module](https://nixos.wiki/wiki/Module)
which ___does not___ declare any new
[module options](https://nixos.org/manual/nixos/stable/index.html#sec-option-declarations).
If you need to do that, use the [modules directory](../modules).

These restrictions help simplify the import logic used to pass profles to
[suites](../suites).
> ##### _Note:_
> [hercules-ci](../doc/integrations/hercules.md) expects all profiles to be
> defined in a _default.nix_. Similarly, [suites](../suites) expect a
> _default.nix_ as well.

### Example
#### Correct ✔
Expand Down Expand Up @@ -40,6 +42,25 @@ program configurations such as your text editor, and shell configs. This way,
you can either pull in the whole development profile, or pick and choose
individual programs.

### Example

profiles/develop/default.nix:
```nix
{
imports = [ ./zsh ];
# some generic development concerns ...
}
```

profiles/develop/zsh/default.nix:
```nix
{ ... }:
{
programs.zsh.enable = true;
# zsh specific options ...
}
```

## Conclusion
Profiles are the most important concept in devos. They allow us to keep our
nix expressions self contained and modular. This way we can maximize reuse
Expand Down
1 change: 0 additions & 1 deletion profiles/user

This file was deleted.

20 changes: 10 additions & 10 deletions suites/default.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{ lib }:
let
inherit (builtins) mapAttrs isFunction;
inherit (lib.flk) importDefaults;
inherit (lib.flk) mkProfileAttrs profileMap;

profiles = importDefaults (toString ../profiles);
users = importDefaults (toString ../users);
profiles = mkProfileAttrs (toString ../profiles);
users = mkProfileAttrs (toString ../users);

allProfiles =
let
sansCore = lib.filterAttrs (n: _: n != "core") profiles;
in
lib.collect isFunction sansCore;
let defaults = lib.collect (x: x ? default) profiles;
in map (x: x.default) defaults;

allUsers = lib.collect isFunction users;
allUsers =
let defaults = lib.collect (x: x ? default) users;
in map (x: x.default) defaults;


suites = with profiles; rec {
core = [ users.nixos users.root ];
base = [ users.nixos users.root ];
};
in
mapAttrs (_: v: lib.flk.profileMap v) suites // {
mapAttrs (_: v: profileMap v) suites // {
inherit allProfiles allUsers;
}