Skip to content

Commit

Permalink
feat: introduce development templates for easier project setup
Browse files Browse the repository at this point in the history
  • Loading branch information
BastianAsmussen committed Jan 29, 2025
1 parent 376e09e commit a9e13b6
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 17 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This is a repository for my NixOS configuration.
- [Installation Guide](#installation-guide)
- [Maintenance Guide](#maintenance-guide)
- [To-Do Tracking](#to-do-tracking)
- [Development Templates](#development-templates)

## Installation Guide

Expand Down Expand Up @@ -88,3 +89,30 @@ setup in a file called [TODO.md](./TODO.md).
If you have suggestions or notice something that could be improved, feel free
to open a pull request. I'll review and consider integrating your
contributions.

## Development Templates

You can use this flake for development environment templates.

### List Templates

```sh
nix shell nixpkgs#jq --command sh "nix flake show self --all-systems --json | jq .templates"
```

### Use Template

> [!NOTE]
> Because we override the [Nix registry](https://nix.dev/manual/nix/2.18/command-ref/new-cli/nix3-registry#description)
> [here](./modules/nixos/nix/default.nix), we can simply use the `self` registry
> entry which references this flake.

### Rust Example

```sh
mkdir ~/Projects/template
cd ~/Projects/template
nix flake init -t self#rust
```

27 changes: 11 additions & 16 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ around problems.

## Table of Contents

- [Developer Environments](#developer-environments)
- [Development Environments](#development-environments)
- [Neovim](#neovim)
- [Impermanence Setup](#impermanence-setup)
- [AGS Migration](#ags-migration)
Expand All @@ -15,23 +15,18 @@ around problems.
- [Secrets Management](#secrets-management)
- [SSH](#ssh)

## Developer Environments
## Development Environments

I want a proper way to handle ephemeral developer environments. Right now I'm
using [devenv](./modules/home-manager/terminal/direnv.nix), but it feels a bit
like cheating; what I'd really like is to add templates to my flake.
I've started using [Nix Flake Templates](https://nix.dev/manual/nix/2.24/command-ref/new-cli/nix3-flake-init.html#description)
to handle development environments and it seems pretty good! Now I'll just have
to implement a template for each language I use, which, as of now consists of:

### Inspiration

I've seen a repository called
[nix-templates](https://github.com/MordragT/nix-templates) which might be
pretty useful. Perhaps it could be a flake input?

### Distrobox

I'm also using [Distrobox](https://distrobox.it) for generic Linux environments.
Package installs sometimes don't work as expected due to FHS issues. I'll need
to find a fix for this.
- [x] Rust
- [ ] Go
- [ ] C#
- [ ] Haskell
- [ ] C
- [ ] Python

## Neovim

Expand Down
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@
icon = ./assets/icons/bastian.png;
};
in {
packages = forAllSystems ({pkgs}: import ./pkgs {inherit pkgs;});
overlays = import ./overlays {inherit inputs lib;};
packages = forAllSystems ({pkgs}: import ./pkgs {inherit pkgs;});
formatter = forAllSystems ({pkgs}: pkgs.alejandra);
checks = forAllSystems ({pkgs}: {
default = pkgs.callPackage ./tests {inherit pkgs lib;};
});

templates = import ./templates;
devShells = forAllSystems ({pkgs}: {
default = import ./shell.nix {inherit pkgs;};
});
Expand Down
18 changes: 18 additions & 0 deletions templates/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
rec {
default = rust;
rust = {
path = ./rust;
description = "Standard Rust development environment.";
welcomeText = ''
# Rust Project Template
## Intended Usage
Development and packaging of Rust programs and libraries.
## Notes
Do not forget to change the `Cargo.toml`'s `package.name` field!
'';
};
}
4 changes: 4 additions & 0 deletions templates/rust/.cargo/audit.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Doesn't work in the sandbox.
[yanked]
enabled = false # Don't warn for yanked crates in Cargo.lock.
update_index = false # Don't auto-update the crates.io index.
1 change: 1 addition & 0 deletions templates/rust/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
4 changes: 4 additions & 0 deletions templates/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.direnv

result*
target/
7 changes: 7 additions & 0 deletions templates/rust/Cargo.lock

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

7 changes: 7 additions & 0 deletions templates/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "template"
version = "0.1.0"
edition = "2021"
license-file = "LICNESE"

[dependencies]
23 changes: 23 additions & 0 deletions templates/rust/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

Copyright (c) 2025 Bastian Asmussen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


23 changes: 23 additions & 0 deletions templates/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Rust Project Template

This project is built using [Nix](https://nixos.org).

## Usage

### Building

```sh
nix build
```

### Testing

```sh
nix flake check --all-systems
```

### Running

```sh
nix run
```
2 changes: 2 additions & 0 deletions templates/rust/deny.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[licenses]
allow = ["MIT"]
137 changes: 137 additions & 0 deletions templates/rust/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
fenix = {
url = "github:nix-community/fenix";
inputs = {
nixpkgs.follows = "nixpkgs";
rust-analyzer-src.follows = "";
};
};

advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
};

outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};

inherit (pkgs) lib;

craneLib = crane.mkLib pkgs;
src = ./.;

# Common arguments can be set here to avoid repeating them later.
commonArgs = {
inherit src;

strictDeps = true;
buildInputs = [
# Additional build inputs.
] ++ lib.optionals pkgs.stdenv.isDarwin (with pkgs; [
libiconv
]);

# Additional environment variables can be set directly.
# RUST_BACKTRACE = "1";
};

craneLibLLvmTools = craneLib.overrideToolchain
(fenix.packages.${system}.complete.withComponents [
"cargo"
"llvm-tools"
"rustc"
]);

# Build *just* the cargo dependencies, so we can reuse all of that work
# (e.g. via cachix) when running in CI.
cargoArtifacts = craneLib.buildDepsOnly commonArgs;

# Build the actual crate itself, reusing the dependency artifacts from
# above.
crate = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
});
in
{
checks = {
# Build the crate as part of `nix flake check` for convenience.
inherit crate;

# Run clippy (and deny all warnings) on the crate source, again,
# reusing the dependency artifacts from above.
#
# Note that this is done as a separate derivation so that we can block
# the CI if there are issues here, but not prevent downstream
# consumers from building our crate by itself.
crate-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;

cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});

crate-doc = craneLib.cargoDoc (commonArgs // {
inherit cargoArtifacts;
});

# Check formatting.
crate-fmt = craneLib.cargoFmt {
inherit src;
};

crate-toml-fmt = craneLib.taploFmt {
src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ];
};

# Audit dependencies.
crate-audit = craneLib.cargoAudit {
inherit src advisory-db;
};

# Audit licenses.
crate-deny = craneLib.cargoDeny {
inherit src;
};

# Run tests with `cargo-nextest`.
crate-nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;

partitions = 1;
partitionType = "count";
cargoNextestPartitionsExtraArgs = "--no-tests=pass";
});
};

packages = {
default = crate;
} // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
crate-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
inherit cargoArtifacts;
});
};

apps.default = flake-utils.lib.mkApp {
drv = crate;
};

devShells.default = craneLib.devShell {
# Inherit inputs from checks.
checks = self.checks.${system};

# Additional dev-shell environment variables can be set here directly.
# MY_CUSTOM_DEVELOPMENT_VAR = "something else";

# Extra inputs can be added here; cargo and rustc are provided by default.
# packages = with pkgs; [
# bacon
# ];
};
});
}
3 changes: 3 additions & 0 deletions templates/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, World!");
}
9 changes: 9 additions & 0 deletions templates/rust/taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[formatting]
reorder_keys = false

[[rule]]
include = ["**/Cargo.toml"]
keys = ["dependencies"]

[rule.formatting]
reorder_keys = true

0 comments on commit a9e13b6

Please sign in to comment.