Skip to content

Commit 1f3cde6

Browse files
committed
Merge #22: Do some clean up of the Nix infrastructure
Approved-by: isomorpheme Priority: Normal Auto-deploy: false
2 parents b4fb822 + 30ba761 commit 1f3cde6

File tree

12 files changed

+102
-259
lines changed

12 files changed

+102
-259
lines changed

.envrc

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
# shellcheck disable=SC2148 # .envrc files need no shell directive
22
shopt -s dotglob globstar
33

4-
# NOTE: This file is based on https://github.com/channable/repository-template/blob/master/.envrc
5-
# When making edits that apply to other repositories, please update the file there.
6-
7-
# Add possibility to run a custom envrc that completely overrides the behavior of this envrc.
8-
CUSTOM_ENVRC=.customenvrc
9-
if [ -f "$CUSTOM_ENVRC" ]; then
10-
echo "Using .customenvrc file"
11-
source_env $CUSTOM_ENVRC
12-
else
13-
# Decrease logging output
14-
# shellcheck disable=SC2034 # unused variable is still read by direnv.
15-
DIRENV_LOG_FORMAT=
16-
# Install nix-direnv, which has an improved implementation of `use nix` that
17-
# caches the Nix environment. Note that this URL is cached locally, so it
18-
# doesn't fetch the script every time.
19-
if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
4+
# Decrease logging output
5+
# shellcheck disable=SC2034 # unused variable is still read by direnv.
6+
DIRENV_LOG_FORMAT=
7+
# Install nix-direnv, which has an improved implementation of `use nix` that
8+
# caches the Nix environment. Note that this URL is cached locally, so it
9+
# doesn't fetch the script every time.
10+
if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
2011
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4="
21-
fi
22-
23-
watch_file nix/sources.json nix/sources.nix nix/overlay.nix nix/python-overlay.nix opsqueue/opsqueue.nix /libs/opsqueue_python/opsqueue_python.nix
12+
fi
2413

14+
watch_file ./nix/ ./**/*.nix
2515

26-
use nix default.nix --argstr environment shell
27-
fi
16+
use nix default.nix --argstr environment shell

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Pre-commit config as used by ./build.py.
21
repos:
32
# Use local to make sure the dependencies are taken from pre-commit's own environment and
43
# therefore from our nix derivation.

default.nix

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,40 @@ let
2222
]
2323
);
2424

25+
# We choose a minimal Rust channel to keep the Nix closure size smaller
26+
rust = pkgs.rust-bin.stable.latest.minimal.override {
27+
extensions = [
28+
"clippy"
29+
"rustfmt"
30+
];
31+
};
32+
2533
defaultEnv = pkgs.buildEnv {
2634
name = "opsqueue-env-default";
27-
paths = with pkgs; [
35+
paths = [
2836
# Command runner
29-
just
37+
pkgs.just
3038

3139
# For linting and formatting
32-
pre-commit
33-
pre-commit-env
40+
pkgs.biome
41+
pkgs.nixfmt-rfc-style
42+
pkgs.pre-commit
43+
pkgs.python3Packages.pre-commit-hooks
44+
pkgs.ruff
3445

3546
# For compiling the Rust parts
36-
rust-with-lsp
37-
sqlx-cli
47+
rust
48+
pkgs.sqlx-cli
3849

3950
# Manage nix pins
40-
niv
41-
nvd
51+
pkgs.niv
52+
pkgs.nvd
4253

4354
# Rust build tools
44-
cargo-audit
45-
cargo-edit
46-
cargo-nextest
47-
maturin
55+
pkgs.cargo-audit
56+
pkgs.cargo-edit
57+
pkgs.cargo-nextest
58+
pkgs.maturin
4859
];
4960
};
5061
environments = {

libs/opsqueue_python/opsqueue_python.nix

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
fileFilter,
2+
lib,
33
buildPythonPackage,
44
rustPlatform,
55
perl,
@@ -10,14 +10,18 @@
1010
opentelemetry-exporter-otlp,
1111
opentelemetry-sdk,
1212
}:
13+
let
14+
root = ../../.;
15+
util = import (root + /nix/util.nix) { inherit lib; };
16+
in
1317
buildPythonPackage rec {
1418
pname = "opsqueue";
1519
version = "0.1.0";
1620
pyproject = true;
1721

18-
src = fileFilter {
22+
src = util.fileFilter {
1923
name = "opsqueue_python";
20-
src = ../../.;
24+
src = root;
2125

2226
# We're copying slightly too much to the Nix store here,
2327
# but using the more granular file filter was very error-prone.

nix/install

Lines changed: 0 additions & 94 deletions
This file was deleted.

nix/overlay.nix

Lines changed: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,16 @@
1-
# Overlay for Nixpkgs which holds rust-jobs related packages.
2-
#
3-
# Serves as common overlay for this repository.
4-
#
5-
self: super:
1+
# Overlay for Nixpkgs which holds all opsqueue related packages.
2+
final: prev:
63
let
7-
sources = import ./sources.nix;
8-
lib = super.pkgs.lib;
9-
pythonOverlay = import ./python-overlay.nix { inherit sources; };
4+
pythonOverlay = import ./python-overlay.nix;
105
in
116
{
12-
# A powerful way of filtering the right files for the src attribute of a derivation.
13-
fileFilter =
14-
{
15-
name,
16-
# Regex whitelist of files
17-
srcWhitelist ? [ ".*" ],
18-
# Blacklist of files and directories anywhere in the repository. Use only
19-
# with files that can appear in multiple places in the repository.
20-
srcGlobalBlacklist ? [ ],
21-
# Global whitelist by suffix, only allow the following file extensions
22-
srcGlobalWhitelist ? [ ],
23-
# Global whitelist by regex, only allow the following file extensions
24-
srcGlobalWhitelistRegex ? [ ],
25-
src,
26-
}:
27-
lib.cleanSourceWith rec {
28-
inherit name src;
29-
filter =
30-
path: type:
31-
let
32-
relativePath = lib.removePrefix (toString src + "/") path;
33-
in
34-
(builtins.any (r: builtins.match r relativePath != null) srcWhitelist)
35-
&&
36-
# Whitelist these files
37-
(
38-
(type == "directory")
39-
|| (
40-
(builtins.length srcGlobalWhitelist != 0)
41-
&& (builtins.any (suffix: lib.hasSuffix suffix relativePath) srcGlobalWhitelist)
42-
|| (
43-
(builtins.length srcGlobalWhitelistRegex != 0)
44-
&& (builtins.any (r: builtins.match r relativePath != null) srcGlobalWhitelistRegex)
45-
)
46-
)
47-
)
48-
&&
49-
# Ignore the files from srcGlobalBlacklist anywhere
50-
!(builtins.elem (baseNameOf path) srcGlobalBlacklist)
51-
&&
52-
# Discard editor files, git repo stuff, and other cruft. This one is taken
53-
# from Nixpkgs; we don't need to implement it ourselves.
54-
lib.cleanSourceFilter path type;
55-
};
56-
57-
# Placing the sources in the overlay gives all packages access to the sources,
58-
# and it makes it possible to override them in new overlays.
59-
sources = if super ? sources then super.sources // sources else sources;
60-
61-
opsqueue = self.callPackage ../opsqueue/opsqueue.nix { };
7+
opsqueue = final.callPackage ../opsqueue/opsqueue.nix { };
628

639
# The explicit choice is made not to override `python312`, as this will cause a rebuild of many
6410
# packages when nixpkgs uses python 3.12 as default python environment.
6511
# These packages should not be affected, e.g. cachix. This is because of a transitive
6612
# dependency on the Python packages that we override.
6713
# In our case cachix > ghc > shpinx > Python libraries.
68-
pythonChannable = super.python312.override { packageOverrides = pythonOverlay; };
69-
70-
# We choose a minimal Rust channel to keep the Nix closure size smaller
71-
rust-with-lsp = self.rust-bin.stable.latest.minimal.override {
72-
extensions = [
73-
"clippy"
74-
"rustfmt"
75-
];
76-
};
14+
pythonChannable = prev.python312.override { packageOverrides = pythonOverlay; };
7715

78-
pre-commit-env = self.buildEnv {
79-
name = "pre-commit-env";
80-
paths = [
81-
super.python3Packages.pre-commit-hooks
82-
super.nixfmt-rfc-style
83-
super.ruff
84-
super.biome
85-
self.rust-with-lsp
86-
];
87-
};
8816
}

nix/python-overlay.nix

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{
2-
sources ? import ./sources.nix,
3-
}:
4-
self: super: { opsqueue_python = self.callPackage ../libs/opsqueue_python/opsqueue_python.nix { }; }
1+
final: prev: {
2+
opsqueue_python = final.callPackage ../libs/opsqueue_python/opsqueue_python.nix { };
3+
}

nix/util.nix

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Utility functions to use in nix code
2+
{ lib }:
3+
{
4+
# A powerful way of filtering the right files for the src attribute of a derivation.
5+
fileFilter =
6+
{
7+
name,
8+
# Regex whitelist of files
9+
srcWhitelist ? [ ".*" ],
10+
# Blacklist of files and directories anywhere in the repository. Use only
11+
# with files that can appear in multiple places in the repository.
12+
srcGlobalBlacklist ? [ ],
13+
# Global whitelist by suffix, only allow the following file extensions
14+
srcGlobalWhitelist ? [ ],
15+
# Global whitelist by regex, only allow the following file extensions
16+
srcGlobalWhitelistRegex ? [ ],
17+
src,
18+
}:
19+
lib.cleanSourceWith rec {
20+
inherit name src;
21+
filter =
22+
path: type:
23+
let
24+
relativePath = lib.removePrefix (toString src + "/") path;
25+
in
26+
(builtins.any (r: builtins.match r relativePath != null) srcWhitelist)
27+
&&
28+
# Whitelist these files
29+
(
30+
(type == "directory")
31+
|| (
32+
(builtins.length srcGlobalWhitelist != 0)
33+
&& (builtins.any (suffix: lib.hasSuffix suffix relativePath) srcGlobalWhitelist)
34+
|| (
35+
(builtins.length srcGlobalWhitelistRegex != 0)
36+
&& (builtins.any (r: builtins.match r relativePath != null) srcGlobalWhitelistRegex)
37+
)
38+
)
39+
)
40+
&&
41+
# Ignore the files from srcGlobalBlacklist anywhere
42+
!(builtins.elem (baseNameOf path) srcGlobalBlacklist)
43+
&&
44+
# Discard editor files, git repo stuff, and other cruft. This one is taken
45+
# from Nixpkgs; we don't need to implement it ourselves.
46+
lib.cleanSourceFilter path type;
47+
};
48+
}

opsqueue/opsqueue.nix

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
2-
fileFilter,
3-
pkgs,
2+
lib,
43
rustPlatform,
54
# Building options
65
buildType ? "release",
@@ -11,6 +10,10 @@
1110
perl,
1211
git,
1312
}:
13+
let
14+
root = ../.;
15+
util = import (root + /nix/util.nix) { inherit lib; };
16+
in
1417
rustPlatform.buildRustPackage {
1518
name = "opsqueue";
1619
inherit
@@ -20,7 +23,7 @@ rustPlatform.buildRustPackage {
2023
useNextest
2124
;
2225

23-
src = fileFilter {
26+
src = util.fileFilter {
2427
name = "opsqueue";
2528
src = ./.;
2629

0 commit comments

Comments
 (0)