flakelight/README.md

221 lines
6.2 KiB
Markdown
Raw Permalink Normal View History

2023-08-28 03:59:54 +03:00
# Flakelight
2023-04-09 03:56:06 +03:00
2023-08-28 03:59:54 +03:00
A modular Nix flake framework for simplifying flake definitions.
2023-08-28 03:59:54 +03:00
## Goals
2023-08-28 03:59:54 +03:00
- Minimize boilerplate needed for flakes
- Support straightforward configuration of all vanilla flake attributes
- Allow sharing common configuration using modules
- What can be done automatically, should be
- Provide good defaults, but let them be changed/disabled
## Features
- Handles generating per-system attributes
- Extensible using the module system
- Given package definitions, generates package and overlay outputs
2023-08-28 04:16:19 +03:00
- Automatically import attributes from nix files in a directory (default `./nix`)
2023-08-28 03:59:54 +03:00
- Builds formatter outputs that can format multiple file types
- Provides outputs/perSystem options for easy migration
## Documentation
See the [API docs](./API_GUIDE.md) for available options and example usage.
2023-08-28 03:59:54 +03:00
## Additional modules
The following modules are also available:
- [flakelight-rust][] for Rust projects
- [flakelight-zig][] for Zig projects
- [flakelight-elisp][] for flakes providing Emacs lisp package(s)
2024-03-03 22:22:35 +03:00
- [flakelight-darwin][] for nix-darwin configs
[flakelight-rust]: https://github.com/accelbread/flakelight-rust
[flakelight-zig]: https://github.com/accelbread/flakelight-zig
[flakelight-elisp]: https://github.com/accelbread/flakelight-elisp
2024-03-03 22:22:35 +03:00
[flakelight-darwin]: https://github.com/cmacrae/flakelight-darwin
2023-08-28 03:59:54 +03:00
2024-02-29 19:54:08 +03:00
## Contact
Feel free to ask for help or other questions in the issues/discussions, or reach
out on Matrix at [#flakelight:nixos.org][matrix-flakelight].
[matrix-flakelight]: https://matrix.to/#/#flakelight:nixos.org
2023-08-28 03:59:54 +03:00
## Examples
### Shell
The following is an example flake.nix for a devshell, using the passed in
nixpkgs. It outputs `devShell.${system}.default` attributes for each configured
2023-12-06 21:42:07 +03:00
system. `systems` can be set to change configured systems from the default.
```nix
{
2023-12-19 08:46:35 +03:00
inputs.flakelight.url = "github:nix-community/flakelight";
2024-01-16 06:05:49 +03:00
outputs = { flakelight, ... }:
2023-12-06 21:42:07 +03:00
flakelight ./. {
devShell.packages = pkgs: [ pkgs.hello pkgs.coreutils ];
};
}
```
With this flake, calling `nix develop` will make `hello` and `coreutils`
available.
To use a different nixpkgs, you can instead use:
2023-08-28 03:59:54 +03:00
```nix
{
inputs = {
2024-06-28 16:59:41 +03:00
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
2023-12-19 08:46:35 +03:00
flakelight.url = "github:nix-community/flakelight";
2023-08-28 03:59:54 +03:00
};
outputs = { flakelight, ... }@inputs:
2023-08-28 03:59:54 +03:00
flakelight ./. {
2024-05-21 06:42:53 +03:00
inherit inputs;
2023-08-28 03:59:54 +03:00
devShell.packages = pkgs: [ pkgs.hello pkgs.coreutils ];
};
}
```
### Rust package
2023-09-14 09:42:48 +03:00
The following is an example flake for a Rust project using `flakelight-rust`,
invoked by using `flakelight-rust`'s wrapper.
2023-08-28 03:59:54 +03:00
Package metadata is taken from the project's `Cargo.toml`.
2023-09-14 09:42:48 +03:00
```nix
{
inputs.flakelight-rust.url = "github:accelbread/flakelight-rust";
outputs = { flakelight-rust, ... }: flakelight-rust ./. { };
}
```
2023-12-06 21:42:07 +03:00
The above flake exports the following:
- Per-system attributes for default systems (`x86_64-linux` and `aarch64-linux`)
- `packages.${system}.default` attributes for each system
- `overlays.default` providing an overlay with the package (built with the
applied pkg set's dependencies)
- `devShells.${system}.default` that provides `rust-analyzer`, `cargo`, `clippy`,
`rustc`, and `rustfmt` as well as sets `RUST_SRC_PATH`
- `checks.${system}.${check}` attributes for build, test, clippy, and formatting
checks
- `formatter.${system}` with additional support for formatting Rust files
Equivalently, you can just import the `flakelight-rust` module as follows:
2023-09-14 09:42:48 +03:00
2023-08-28 03:59:54 +03:00
```nix
{
inputs = {
2023-12-19 08:46:35 +03:00
flakelight.url = "github:nix-community/flakelight";
2023-08-28 03:59:54 +03:00
flakelight-rust.url = "github:accelbread/flakelight-rust";
};
2023-08-28 09:52:33 +03:00
outputs = { flakelight, flakelight-rust, ... }: flakelight ./. {
2023-08-28 03:59:54 +03:00
imports = [ flakelight-rust.flakelightModules.default ];
};
}
```
2023-12-06 21:42:07 +03:00
See [flakelight-rust.nix][flakelight-rust] to see how you could configure it
without the module.
2023-08-28 03:59:54 +03:00
[flakelight-rust]: https://github.com/accelbread/flakelight-rust/blob/master/flakelight-rust.nix
### C application
The following example flake is for a C project with a simple `make` setup.
```nix
{
2023-12-06 21:42:07 +03:00
description = "My C application.";
2023-12-19 08:46:35 +03:00
inputs.flakelight.url = "github:nix-community/flakelight";
outputs = { flakelight, ... }:
2023-08-28 03:59:54 +03:00
flakelight ./. {
license = "AGPL-3.0-or-later";
package = { stdenv, defaultMeta }:
stdenv.mkDerivation {
name = "hello-world";
src = ./.;
installPhase = ''
runHook preInstall
make DESTDIR=$out install
runHook postInstall
'';
meta = defaultMeta;
};
devShell.packages = pkgs: with pkgs; [ clang-tools coreutils ];
2023-12-06 21:42:07 +03:00
formatters = {
"*.h" = "clang-format -i";
"*.c" = "clang-format -i";
}
2023-08-28 03:59:54 +03:00
};
}
```
This flake exports the following:
- Per-system attributes for default systems (`x86_64-linux` and `aarch64-linux`)
2023-12-06 21:42:07 +03:00
- `packages.${system}.default` attributes for each system, with license and
description set
2023-08-28 03:59:54 +03:00
- `overlays.default` providing an overlay with the package (built with the
applied pkg set's dependencies)
2023-12-06 21:42:07 +03:00
- `devShells.${system}.default` that provides `clang-tools` and `coreutils`
2023-08-28 03:59:54 +03:00
- `checks.${system}.${check}` attributes for build and formatting checks.
- `formatter.${system}` with additional support for formatting `c` and `h` files
2023-12-06 21:42:07 +03:00
with `clang-format`
2023-08-28 04:16:19 +03:00
### C application using autoloads
The above example can instead use the autoload directory feature for the package
like the following. Most attributes can be autoloaded.
`./flake.nix`:
```nix
{
2023-12-06 21:42:07 +03:00
description = "My C application.";
2023-12-19 08:46:35 +03:00
inputs.flakelight.url = "github:nix-community/flakelight";
outputs = { flakelight, ... }:
2023-08-28 04:16:19 +03:00
flakelight ./. {
license = "AGPL-3.0-or-later";
devShell.packages = pkgs: with pkgs; [ clang-tools coreutils ];
2023-12-06 21:42:07 +03:00
formatters = {
"*.h" = "clang-format -i";
"*.c" = "clang-format -i";
}
2023-08-28 04:16:19 +03:00
};
}
```
`./nix/packages/_default.nix`:
```nix
{ stdenv, defaultMeta }:
stdenv.mkDerivation {
name = "hello-world";
src = ./.;
installPhase = ''
runHook preInstall
make DESTDIR=$out install
runHook postInstall
'';
meta = defaultMeta;
}
```
2023-12-06 21:42:07 +03:00
A leading underscore in filename is stripped (default needs to be escaped to not
conflict with dir import).
## Related Resources
- [Comparison to flake-parts](https://discourse.nixos.org/t/flakelight-a-new-modular-flake-framework/32395/3)