Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
# shellcheck disable=SC2148 # .envrc files need no shell directive
shopt -s dotglob globstar

# NOTE: This file is based on https://github.com/channable/repository-template/blob/master/.envrc
# When making edits that apply to other repositories, please update the file there.

# Add possibility to run a custom envrc that completely overrides the behavior of this envrc.
CUSTOM_ENVRC=.customenvrc
if [ -f "$CUSTOM_ENVRC" ]; then
echo "Using .customenvrc file"
source_env $CUSTOM_ENVRC
else
# Decrease logging output
# shellcheck disable=SC2034 # unused variable is still read by direnv.
DIRENV_LOG_FORMAT=
# Install nix-direnv, which has an improved implementation of `use nix` that
# caches the Nix environment. Note that this URL is cached locally, so it
# doesn't fetch the script every time.
if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
# Decrease logging output
# shellcheck disable=SC2034 # unused variable is still read by direnv.
DIRENV_LOG_FORMAT=
# Install nix-direnv, which has an improved implementation of `use nix` that
# caches the Nix environment. Note that this URL is cached locally, so it
# doesn't fetch the script every time.
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/sources.json nix/sources.nix nix/overlay.nix nix/python-overlay.nix opsqueue/opsqueue.nix /libs/opsqueue_python/opsqueue_python.nix
fi

watch_file ./nix/ ./**/*.nix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍


use nix default.nix --argstr environment shell
fi
use nix default.nix --argstr environment shell
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Pre-commit config as used by ./build.py.
repos:
# Use local to make sure the dependencies are taken from pre-commit's own environment and
# therefore from our nix derivation.
Expand Down
35 changes: 23 additions & 12 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,40 @@ let
]
);

# We choose a minimal Rust channel to keep the Nix closure size smaller
rust = pkgs.rust-bin.stable.latest.minimal.override {
extensions = [
"clippy"
"rustfmt"
];
};

defaultEnv = pkgs.buildEnv {
name = "opsqueue-env-default";
paths = with pkgs; [
paths = [
# Command runner
just
pkgs.just

# For linting and formatting
pre-commit
pre-commit-env
pkgs.biome
pkgs.nixfmt-rfc-style
pkgs.pre-commit
pkgs.python3Packages.pre-commit-hooks
pkgs.ruff

# For compiling the Rust parts
rust-with-lsp
sqlx-cli
rust
pkgs.sqlx-cli

# Manage nix pins
niv
nvd
pkgs.niv
pkgs.nvd

# Rust build tools
cargo-audit
cargo-edit
cargo-nextest
maturin
pkgs.cargo-audit
pkgs.cargo-edit
pkgs.cargo-nextest
pkgs.maturin
];
};
environments = {
Expand Down
10 changes: 7 additions & 3 deletions libs/opsqueue_python/opsqueue_python.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
fileFilter,
lib,
buildPythonPackage,
rustPlatform,
perl,
Expand All @@ -10,14 +10,18 @@
opentelemetry-exporter-otlp,
opentelemetry-sdk,
}:
let
root = ../../.;
util = import (root + /nix/util.nix) { inherit lib; };
in
buildPythonPackage rec {
pname = "opsqueue";
version = "0.1.0";
pyproject = true;

src = fileFilter {
src = util.fileFilter {
name = "opsqueue_python";
src = ../../.;
src = root;

# We're copying slightly too much to the Nix store here,
# but using the more granular file filter was very error-prone.
Expand Down
94 changes: 0 additions & 94 deletions nix/install

This file was deleted.

82 changes: 5 additions & 77 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
@@ -1,88 +1,16 @@
# Overlay for Nixpkgs which holds rust-jobs related packages.
#
# Serves as common overlay for this repository.
#
self: super:
# Overlay for Nixpkgs which holds all opsqueue related packages.
final: prev:
let
sources = import ./sources.nix;
lib = super.pkgs.lib;
pythonOverlay = import ./python-overlay.nix { inherit sources; };
pythonOverlay = import ./python-overlay.nix;
in
{
# A powerful way of filtering the right files for the src attribute of a derivation.
fileFilter =
{
name,
# Regex whitelist of files
srcWhitelist ? [ ".*" ],
# Blacklist of files and directories anywhere in the repository. Use only
# with files that can appear in multiple places in the repository.
srcGlobalBlacklist ? [ ],
# Global whitelist by suffix, only allow the following file extensions
srcGlobalWhitelist ? [ ],
# Global whitelist by regex, only allow the following file extensions
srcGlobalWhitelistRegex ? [ ],
src,
}:
lib.cleanSourceWith rec {
inherit name src;
filter =
path: type:
let
relativePath = lib.removePrefix (toString src + "/") path;
in
(builtins.any (r: builtins.match r relativePath != null) srcWhitelist)
&&
# Whitelist these files
(
(type == "directory")
|| (
(builtins.length srcGlobalWhitelist != 0)
&& (builtins.any (suffix: lib.hasSuffix suffix relativePath) srcGlobalWhitelist)
|| (
(builtins.length srcGlobalWhitelistRegex != 0)
&& (builtins.any (r: builtins.match r relativePath != null) srcGlobalWhitelistRegex)
)
)
)
&&
# Ignore the files from srcGlobalBlacklist anywhere
!(builtins.elem (baseNameOf path) srcGlobalBlacklist)
&&
# Discard editor files, git repo stuff, and other cruft. This one is taken
# from Nixpkgs; we don't need to implement it ourselves.
lib.cleanSourceFilter path type;
};

# Placing the sources in the overlay gives all packages access to the sources,
# and it makes it possible to override them in new overlays.
sources = if super ? sources then super.sources // sources else sources;

opsqueue = self.callPackage ../opsqueue/opsqueue.nix { };
opsqueue = final.callPackage ../opsqueue/opsqueue.nix { };

# The explicit choice is made not to override `python312`, as this will cause a rebuild of many
# packages when nixpkgs uses python 3.12 as default python environment.
# These packages should not be affected, e.g. cachix. This is because of a transitive
# dependency on the Python packages that we override.
# In our case cachix > ghc > shpinx > Python libraries.
pythonChannable = super.python312.override { packageOverrides = pythonOverlay; };

# We choose a minimal Rust channel to keep the Nix closure size smaller
rust-with-lsp = self.rust-bin.stable.latest.minimal.override {
extensions = [
"clippy"
"rustfmt"
];
};
pythonChannable = prev.python312.override { packageOverrides = pythonOverlay; };

pre-commit-env = self.buildEnv {
name = "pre-commit-env";
paths = [
super.python3Packages.pre-commit-hooks
super.nixfmt-rfc-style
super.ruff
super.biome
self.rust-with-lsp
];
};
}
7 changes: 3 additions & 4 deletions nix/python-overlay.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
sources ? import ./sources.nix,
}:
self: super: { opsqueue_python = self.callPackage ../libs/opsqueue_python/opsqueue_python.nix { }; }
final: prev: {
opsqueue_python = final.callPackage ../libs/opsqueue_python/opsqueue_python.nix { };
}
48 changes: 48 additions & 0 deletions nix/util.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Utility functions to use in nix code
{ lib }:
{
# A powerful way of filtering the right files for the src attribute of a derivation.
fileFilter =
{
name,
# Regex whitelist of files
srcWhitelist ? [ ".*" ],
# Blacklist of files and directories anywhere in the repository. Use only
# with files that can appear in multiple places in the repository.
srcGlobalBlacklist ? [ ],
# Global whitelist by suffix, only allow the following file extensions
srcGlobalWhitelist ? [ ],
# Global whitelist by regex, only allow the following file extensions
srcGlobalWhitelistRegex ? [ ],
src,
}:
lib.cleanSourceWith rec {
inherit name src;
filter =
path: type:
let
relativePath = lib.removePrefix (toString src + "/") path;
in
(builtins.any (r: builtins.match r relativePath != null) srcWhitelist)
&&
# Whitelist these files
(
(type == "directory")
|| (
(builtins.length srcGlobalWhitelist != 0)
&& (builtins.any (suffix: lib.hasSuffix suffix relativePath) srcGlobalWhitelist)
|| (
(builtins.length srcGlobalWhitelistRegex != 0)
&& (builtins.any (r: builtins.match r relativePath != null) srcGlobalWhitelistRegex)
)
)
)
&&
# Ignore the files from srcGlobalBlacklist anywhere
!(builtins.elem (baseNameOf path) srcGlobalBlacklist)
&&
# Discard editor files, git repo stuff, and other cruft. This one is taken
# from Nixpkgs; we don't need to implement it ourselves.
lib.cleanSourceFilter path type;
};
}
9 changes: 6 additions & 3 deletions opsqueue/opsqueue.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
fileFilter,
pkgs,
lib,
rustPlatform,
# Building options
buildType ? "release",
Expand All @@ -11,6 +10,10 @@
perl,
git,
}:
let
root = ../.;
util = import (root + /nix/util.nix) { inherit lib; };
in
rustPlatform.buildRustPackage {
name = "opsqueue";
inherit
Expand All @@ -20,7 +23,7 @@ rustPlatform.buildRustPackage {
useNextest
;

src = fileFilter {
src = util.fileFilter {
name = "opsqueue";
src = ./.;

Expand Down
Loading
Loading