Skip to content

Commit

Permalink
Better flake template
Browse files Browse the repository at this point in the history
  • Loading branch information
kolloch committed Jan 7, 2024
1 parent b1552fd commit c6d3406
Show file tree
Hide file tree
Showing 16 changed files with 651 additions and 73 deletions.
9 changes: 7 additions & 2 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/usr/bin/env bash
# ^ make editor happy

#
# Use https://direnv.net/ to automatically load the dev shell.
#

if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4="
fi

watch_file nix/**
watch_file -- **/*.nix
# Adding files to git includes them in a flake
watch_file .git/index .git/HEAD
use flake
# But it is also a bit much reloading.
# watch_file .git/index .git/HEAD
use flake . --show-trace
10 changes: 7 additions & 3 deletions crate2nix/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
{ pkgs
, ...
}@perSystem: {
devshells.default = {
# imports = [
# inputs.pre-commit-hooks.flakeModule
# ];

config.devshells.default = {
imports = [
"${inputs.devshell}/extra/language/c.nix"
"${inputs.devshell}/extra/language/rust.nix"
Expand All @@ -28,7 +32,7 @@
};
};

pre-commit = {
config.pre-commit = {
settings.settings.rust.cargoManifestPath = "${self}/crate2nix/Cargo.toml";

settings.hooks = {
Expand All @@ -38,6 +42,6 @@
};
};

packages.default = pkgs.callPackage ./default.nix { };
config.packages.default = pkgs.callPackage ./default.nix { };
};
}
5 changes: 4 additions & 1 deletion docs/src/content/docs/00_guides/10_flake_template_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ If the directory is empty, it will also create a hello world stub.

If you call this from an existing project, make sure to delete any superfluous
files. The template expects top-level `Cargo.toml`/`Cargo.lock` files,
otherwise you need to adjust `flake.nix` manually.
and a single result binary, otherwise you need to adjust `flake.nix` manually.

Continue to use `cargo`/`rust-analyzer` for a fast developer loop and use
nix to verify your program hermetically with nix locally and in CI:

```bash title="Building project and running tests"
nix build
Expand Down
9 changes: 7 additions & 2 deletions docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { Card, CardGrid } from '@astrojs/starlight/components';
import { Icon } from '@astrojs/starlight/components';

<Card title="What does crate2nix do?">
`crate2nix` generates [nix](https://nixos.org/nix/) build files for [rust](https://www.rust-lang.org/) crates
using [cargo](https://crates.io/) so that you can **build your rust project with nix**.
`crate2nix` builds your [cargo](https://crates.io/)-based [rust](https://www.rust-lang.org/) project
crate-by-crate with [nix](https://nixos.org/nix/).

You can

* save time by only rebuilding changed crates hermetically in CI, and
* use `cargo`/`rust-analyzer` locally for a fast developing loop.
</Card>

**Dependency tree from cargo**: It uses [cargo_metadata](https://github.com/oli-obk/cargo_metadata) to obtain the
Expand Down
12 changes: 8 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
nixConfig = {
extra-trusted-public-keys = "eigenvalue.cachix.org-1:ykerQDDa55PGxU25CETy9wF6uVDpadGGXYrFNJA3TUs=";
extra-substituters = "https://eigenvalue.cachix.org";
allow-import-from-derivation = true;
};

inputs = {
Expand Down Expand Up @@ -64,9 +65,12 @@
];

flake = { lib, ... }: {
config.templates.default = {
path = ./template;
description = "An example of crate2nix";
config.templates = rec {
default = flake-binary;
flake-binary = {
path = ./templates/flake-binary;
description = "An example of crate2nix";
};
};

config.lib = {
Expand Down Expand Up @@ -99,7 +103,7 @@
};
};

perSystem = { config, self', inputs', pkgs, system, ... }: {
perSystem = { config, pkgs, ... }: {
formatter = pkgs.nixpkgs-fmt;
checks = config.packages;
};
Expand Down
61 changes: 0 additions & 61 deletions template/flake.nix

This file was deleted.

17 changes: 17 additions & 0 deletions templates/flake-binary/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# ^ make editor happy

#
# Use https://direnv.net/ to automatically load the dev shell.
#

if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4="
fi

watch_file nix/**
watch_file -- **/*.nix
# Adding files to git includes them in a flake
# But it is also a bit much reloading.
# watch_file .git/index .git/HEAD
use flake . --show-trace
22 changes: 22 additions & 0 deletions templates/flake-binary/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
target
**/*.rs.bk
.idea
*.iml
/result*
*.log
*~

# cachix tmp file
store-path-pre-build

# Devenv
.devenv*
devenv.local.nix

# direnv
.direnv

# pre-commit
.pre-commit-config.yaml

template/flake.lock
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit c6d3406

Please sign in to comment.