flakelight/README.md

217 lines
6.1 KiB
Markdown
Raw Normal View History

2023-08-28 02:59:54 +02:00
# Flakelight
2023-04-09 02:56:06 +02:00
2023-08-28 02:59:54 +02:00
A modular Nix flake framework for simplifying flake definitions.
2023-08-28 02:59:54 +02:00
## Goals
2023-08-28 02:59:54 +02: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 03:16:19 +02:00
- Automatically import attributes from nix files in a directory (default `./nix`)
2023-08-28 02:59:54 +02: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 02:59:54 +02: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 20:22:35 +01: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 20:22:35 +01:00
[flakelight-darwin]: https://github.com/cmacrae/flakelight-darwin
2023-08-28 02:59:54 +02:00
2024-02-29 17:54:08 +01: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 02:59:54 +02: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 19:42:07 +01:00
system. `systems` can be set to change configured systems from the default.
```nix
{
2023-12-19 06:46:35 +01:00
inputs.flakelight.url = "github:nix-community/flakelight";
2024-01-16 04:05:49 +01:00
outputs = { flakelight, ... }:
2023-12-06 19:42:07 +01: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 02:59:54 +02:00
```nix
{
inputs = {
nixpkgs.url = "nixpkgs/nixpkgs-unstable";
2023-12-19 06:46:35 +01:00
flakelight.url = "github:nix-community/flakelight";
2023-08-28 02:59:54 +02:00
};
outputs = { flakelight, ... }:
2023-08-28 02:59:54 +02:00
flakelight ./. {
2024-05-21 05:42:53 +02:00
inherit inputs;
2023-08-28 02:59:54 +02:00
devShell.packages = pkgs: [ pkgs.hello pkgs.coreutils ];
};
}
```
### Rust package
2023-09-14 08:42:48 +02:00
The following is an example flake for a Rust project using `flakelight-rust`,
invoked by using `flakelight-rust`'s wrapper.
2023-08-28 02:59:54 +02:00
Package metadata is taken from the project's `Cargo.toml`.
2023-09-14 08:42:48 +02:00
```nix
{
inputs.flakelight-rust.url = "github:accelbread/flakelight-rust";
outputs = { flakelight-rust, ... }: flakelight-rust ./. { };
}
```
2023-12-06 19:42:07 +01: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 08:42:48 +02:00
2023-08-28 02:59:54 +02:00
```nix
{
inputs = {
2023-12-19 06:46:35 +01:00
flakelight.url = "github:nix-community/flakelight";
2023-08-28 02:59:54 +02:00
flakelight-rust.url = "github:accelbread/flakelight-rust";
};
2023-08-28 08:52:33 +02:00
outputs = { flakelight, flakelight-rust, ... }: flakelight ./. {
2023-08-28 02:59:54 +02:00
imports = [ flakelight-rust.flakelightModules.default ];
};
}
```
2023-12-06 19:42:07 +01:00
See [flakelight-rust.nix][flakelight-rust] to see how you could configure it
without the module.
2023-08-28 02:59:54 +02: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 19:42:07 +01:00
description = "My C application.";
2023-12-19 06:46:35 +01:00
inputs.flakelight.url = "github:nix-community/flakelight";
outputs = { flakelight, ... }:
2023-08-28 02:59:54 +02: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 19:42:07 +01:00
formatters = {
"*.h" = "clang-format -i";
"*.c" = "clang-format -i";
}
2023-08-28 02:59:54 +02:00
};
}
```
This flake exports the following:
- Per-system attributes for default systems (`x86_64-linux` and `aarch64-linux`)
2023-12-06 19:42:07 +01:00
- `packages.${system}.default` attributes for each system, with license and
description set
2023-08-28 02:59:54 +02:00
- `overlays.default` providing an overlay with the package (built with the
applied pkg set's dependencies)
2023-12-06 19:42:07 +01:00
- `devShells.${system}.default` that provides `clang-tools` and `coreutils`
2023-08-28 02:59:54 +02:00
- `checks.${system}.${check}` attributes for build and formatting checks.
- `formatter.${system}` with additional support for formatting `c` and `h` files
2023-12-06 19:42:07 +01:00
with `clang-format`
2023-08-28 03:16:19 +02: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 19:42:07 +01:00
description = "My C application.";
2023-12-19 06:46:35 +01:00
inputs.flakelight.url = "github:nix-community/flakelight";
2023-08-28 03:16:19 +02:00
outputs = { flakelight, ... }@inputs:
flakelight ./. {
license = "AGPL-3.0-or-later";
devShell.packages = pkgs: with pkgs; [ clang-tools coreutils ];
2023-12-06 19:42:07 +01:00
formatters = {
"*.h" = "clang-format -i";
"*.c" = "clang-format -i";
}
2023-08-28 03:16:19 +02: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 19:42:07 +01:00
A leading underscore in filename is stripped (default needs to be escaped to not
conflict with dir import).