Skip to content

Instantly share code, notes, and snippets.

@arianvp
Created August 3, 2020 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arianvp/9e6af68da4dcdcf6402d8d46a5583176 to your computer and use it in GitHub Desktop.
Save arianvp/9e6af68da4dcdcf6402d8d46a5583176 to your computer and use it in GitHub Desktop.
/* Evaluate a NixOS configuration using this evaluation of Nixpkgs.
With this function you can write, for example, a package that
depends on a custom virtual machine image.
Parameter: A module, path or list of those that represent the
configuration of the NixOS system to be constructed.
Result: An attribute set containing packages produced by this
evaluation of NixOS, such as toplevel, kernel and
initialRamdisk.
The result can be extended in the modules by defining
extra attributes in system.build.
Alternatively, you may use the result's config and
options attributes to query any option.
Example:
let
myOS = pkgs.nixos ({ lib, pkgs, config, ... }: {
config.services.nginx = {
enable = true;
# ...
};
# Use config.system.build to exports relevant parts of a
# configuration. The runner attribute should not be
# considered a fully general replacement for systemd
# functionality.
config.system.build.run-nginx = config.systemd.services.nginx.runner;
});
in
myOS.run-nginx
Unlike in plain NixOS, the nixpkgs.config and
nixpkgs.system options will be ignored by default. Instead,
nixpkgs.pkgs will have the default value of pkgs as it was
constructed right after invoking the nixpkgs function (e.g. the
value of import <nixpkgs> { overlays = [./my-overlay.nix]; }
but not the value of (import <nixpkgs> {} // { extra = ...; }).
If you do want to use the config.nixpkgs options, you are
probably better off by calling nixos/lib/eval-config.nix
directly, even though it is possible to set config.nixpkgs.pkgs.
For more information about writing NixOS modules, see
https://nixos.org/nixos/manual/index.html#sec-writing-modules
Note that you will need to have called Nixpkgs with the system
parameter set to the right value for your deployment target.
*/
nixos =
configuration:
let
c = import (pkgs.path + "/nixos/lib/eval-config.nix") {
inherit (pkgs.stdenv.hostPlatform) system;
modules =
[(
{ lib, ... }: {
config.nixpkgs.pkgs = lib.mkDefault pkgs;
}
)] ++ (
if builtins.isList configuration
then configuration
else [configuration]
);
};
in
c.config.system.build // c;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment