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

lib: init and use mkFlake and evalFlakeArgs #218

Closed
wants to merge 17 commits into from
Closed
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
160 changes: 160 additions & 0 deletions doc/mkFlakeOptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
## extern
Function with argument 'inputs' that contains all devos and ${self}'s inputs.
The function should return an attribute set with modules, overlays, and
specialArgs to be included across nixos and home manager configurations.
Only attributes that are used should be returned.


*_Type_*:
function that evaluates to a(n) attrs

*_Default_*
```
"{ modules = []; overlays = []; specialArgs = []; userModules = []; userSpecialArgs = []; }\n"
```


## hosts
Path to directory containing host configurations that will be exported
to the 'nixosConfigurations' output.


*_Type_*:
path

*_Default_*
```
"${self}/hosts"
```


## modules
list of modules to include in confgurations and export in 'nixosModules' output


*_Type_*:
list of valid modules

*_Default_*
```
[]
```


## overlays
path to folder containing overlays which will be applied to pkgs and exported in
the 'overlays' output


*_Type_*:
path

*_Default_*
```
"${self}/overlays"
```


## overrides
attrset of packages and modules that will be pulled from nixpkgs master

*_Type_*:
attribute set

*_Default_*
```
"{ modules = []; disabledModules = []; packages = {}; }"
```


## packages
Overlay for custom packages that will be included in treewide 'pkgs'.
This should follow the standard nixpkgs overlay format - two argument function
that returns an attrset.
These packages will be exported to the 'packages' and 'legacyPackages' outputs.


*_Type_*:
Nixpkgs overlay

*_Default_*
```
"(final: prev: {})"
```


## profiles
path to profiles folder that can be collected into suites

*_Type_*:
path

*_Default_*
```
"${self}/profiles"
```


## self
The flake to create the devos outputs for

*_Type_*:
attribute set



## suites
Function with inputs 'users' and 'profiles' that returns attribute set
with user and system suites. The former for Home Manager and the latter
for nixos configurations.
These can be accessed through the 'suites' specialArg in each config system.


*_Type_*:
function that evaluates to a(n) attrs

*_Default_*
```
"{ user = {}; system = {}; }"
```


## userModules
list of modules to include in home-manager configurations and export in
'homeModules' output


*_Type_*:
list of valid modules

*_Default_*
```
[]
```


## userProfiles
path to user profiles folder that can be collected into userSuites

*_Type_*:
path

*_Default_*
```
"${self}/users/profiles"
```


## users
path to folder containing profiles that define system users


*_Type_*:
path

*_Default_*
```
"${self}/users"
```


1 change: 1 addition & 0 deletions doc/start/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ In addition, the [binary cache](../../cachix) is added for faster deployment.
> upstream changes.

## Next Steps:
- [Use Devos as a library!](./mkflake.md)
- [Make installable ISO](./iso.md)
- [Bootstrap Host](./bootstrapping.md)
- [Already on NixOS](./from-nixos.md)
Expand Down
42 changes: 42 additions & 0 deletions doc/start/mkflake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Use Devos as a library!
You can also add devos as a flake input and use its library function, `mkFlake` to
create your flake. This gives you the advantage of using nix flakes to sync with
upstream changes in devos.

You can either use the default template or use the 'mkflake' template which only
includes the necessary folders for `mkFlake` usage. It can be pulled with:
```sh
nix flake init -t github:divnix/devos#mkflake
```

Once you have a template, you need to add devos as a flake input, which would look
like this:
```nix
inputs = {
...
devos.url = "github:divnix/devos";
};
```
> ##### Note:
> - All devos inputs must still be included in your flake, due to a nix
> [issue](https://github.com/NixOS/nix/pull/4641) with the `follows` attribute.
> - You can append `/community` to access community modules [extern](../../extern).

You can then call `mkFlake` to create your outputs. Here is a simple example:
```nix
outputs = { self, devos, ... }: devos.lib.mkFlake {
inherit self;
hosts = ./hosts;
};
```
`mkFlake` has various arguments to include more devos concepts like suites and profiles.
These options are documented in [mkFlakeOptions](../mkFlakeOptions.md).

The devos template itself uses mkFlake to export its own outputs, so you can take
a look at this repository's [flake.nix](../../flake.nix) for a more realistic use
of `mkFlake`.

You can now sync with upstream devos changes just like any other flake input. But
you will no longer be able to edit devos's internals, since you are directly following
upstream devos changes.

2 changes: 1 addition & 1 deletion flake.lock

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

94 changes: 24 additions & 70 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,79 +28,33 @@
pkgs.inputs.nixpkgs.follows = "nixos";
};

outputs = inputs@{ deploy, nixos, nur, self, utils, ... }:
let
inherit (self) lib;
inherit (lib) os;

extern = import ./extern { inherit inputs; };
overrides = import ./overrides;

multiPkgs = os.mkPkgs {
inherit extern overrides;
};

suites = os.mkSuites {
outputs = inputs@{ deploy, nixos, nur, self, utils, ... }:
let
lib = import ./lib { inherit self nixos inputs; };
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we import ./lib as a "subflake", instead?

inputs.devos.path = ./lib;

It makes this just an in-tree user of an otherwise independent lib and makes the step to carve it out into a separate repo absolutely trivial.

Copy link
Member Author

Choose a reason for hiding this comment

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

Overall this is a good idea, but it would require a bit of refactoring that I would rather not include in this PR.

in lib.mkFlake {
inherit self;
hosts = ./hosts;
packages = import ./pkgs;
suites = import ./suites;
users = os.mkProfileAttrs "${self}/users";
profiles = os.mkProfileAttrs "${self}/profiles";
userProfiles = os.mkProfileAttrs "${self}/users/profiles";
};

outputs = {
nixosConfigurations = os.mkHosts {
dir = "${self}/hosts";
overrides = import ./overrides;
inherit multiPkgs suites extern;
};

homeConfigurations = os.mkHomeConfigurations;

nixosModules =
let moduleList = import ./modules/module-list.nix;
in lib.pathsToImportedAttrs moduleList;

homeModules =
let moduleList = import ./users/modules/module-list.nix;
in lib.pathsToImportedAttrs moduleList;

overlay = import ./pkgs;
overlays = lib.pathsToImportedAttrs (lib.pathsIn ./overlays);

lib = import ./lib { inherit nixos self inputs; };

extern = import ./extern;
overrides = import ./overrides;
overlays = ./overlays;
profiles = ./profiles;
userProfiles = ./users/profiles;
modules = import ./modules/module-list.nix;
userModules = import ./users/modules/module-list.nix;
} // {
inherit lib;
defaultTemplate = self.templates.flk;
templates.flk.path = ./.;
templates.flk.description = "flk template";
defaultTemplate = self.templates.flk;

deploy.nodes = os.mkNodes deploy self.nixosConfigurations;
templates.mkflake.path =
let
excludes = [ "lib" "tests" "cachix" "nix" "theme" ".github" "bors.toml" "cachix.nix" ];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

filter = path: type: ! builtins.elem (baseNameOf path) excludes;
in
builtins.filterSource filter ./.;
templates.mkflake.description = "template with necessary folders for mkFlake usage";
};

systemOutputs = utils.lib.eachDefaultSystem (system:
let
pkgs = multiPkgs.${system};
# all packages that are defined in ./pkgs
legacyPackages = os.mkPackages { inherit pkgs; };
in
{
checks =
let
tests = nixos.lib.optionalAttrs (system == "x86_64-linux")
(import ./tests { inherit self pkgs; });
deployHosts = nixos.lib.filterAttrs
(n: _: self.nixosConfigurations.${n}.config.nixpkgs.system == system) self.deploy.nodes;
deployChecks = deploy.lib.${system}.deployChecks { nodes = deployHosts; };
in
nixos.lib.recursiveUpdate tests deployChecks;

inherit legacyPackages;
packages = lib.filterPackages system legacyPackages;

devShell = import ./shell {
inherit self system extern overrides;
};
}
);
in
nixos.lib.recursiveUpdate outputs systemOutputs;
}
19 changes: 9 additions & 10 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
args@{ nixos, self, ... }:
let inherit (nixos) lib; in
lib.makeExtensible (final:
let callLibs = file: import file
({
inherit lib;

dev = final;
} // args);
args@{ nixos, self, ... }: # TODO: craft well-defined api for devos-lib
let
inherit (nixos) lib;
in lib.makeExtensible (final:
let
callLibs = file: import file ({ lib = final; } // args);
in
with final;
{
inherit callLibs;

Expand All @@ -17,6 +13,9 @@ lib.makeExtensible (final:
lists = callLibs ./lists.nix;
strings = callLibs ./strings.nix;

mkFlake = callLibs ./mkFlake;
} //
with final; {
inherit (attrs) mapFilterAttrs genAttrs' safeReadDir
pathsToImportedAttrs concatAttrs filterPackages;
inherit (lists) pathsIn;
Expand Down
20 changes: 10 additions & 10 deletions lib/devos/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, nixos, dev, ... }:
{ lib, ... }:
{
# pkgImport :: Nixpkgs -> Overlays -> System -> Pkgs
pkgImport = nixpkgs: overlays: system:
Expand All @@ -9,22 +9,22 @@

profileMap = map (profile: profile.default);

mkNodes = dev.callLibs ./mkNodes.nix;
mkNodes = lib.callLibs ./mkNodes.nix;

mkHosts = dev.callLibs ./mkHosts.nix;
mkHosts = lib.callLibs ./mkHosts.nix;

mkSuites = dev.callLibs ./mkSuites.nix;
mkSuites = lib.callLibs ./mkSuites.nix;

mkProfileAttrs = dev.callLibs ./mkProfileAttrs.nix;
mkProfileAttrs = lib.callLibs ./mkProfileAttrs.nix;

mkPkgs = dev.callLibs ./mkPkgs.nix;
mkPkgs = lib.callLibs ./mkPkgs.nix;

recImport = dev.callLibs ./recImport.nix;
recImport = lib.callLibs ./recImport.nix;

devosSystem = dev.callLibs ./devosSystem.nix;
devosSystem = lib.callLibs ./devosSystem.nix;

mkHomeConfigurations = dev.callLibs ./mkHomeConfigurations.nix;
mkHomeConfigurations = lib.callLibs ./mkHomeConfigurations.nix;

mkPackages = dev.callLibs ./mkPackages.nix;
mkPackages = lib.callLibs ./mkPackages.nix;
}

Loading