-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add blog post about using a nix flake for development with Loco #907
Open
charludo
wants to merge
1
commit into
loco-rs:master
Choose a base branch
from
charludo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Or maybe it would be nice to add the flake directly, just like the Devcontainer? |
I would also consider swapping to crane I'll paste my current crane file here, if you decide to do that, notes/TODO:
flake.nix{
description = "Build a cargo project";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-analyzer-src.follows = "";
};
flake-utils.url = "github:numtide/flake-utils";
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 = craneLib.cleanCargoSource ./.;
src =
let
yamlFilter = path: _type: builtins.match ".*yaml$" path != null;
snapFilter = path: _type: builtins.match ".*snap$" path != null;
tFilter = path: _type: builtins.match ".*t$" path != null;
customFilter =
path: type:
(yamlFilter path type)
|| (snapFilter path type)
|| (tFilter path type)
|| (craneLib.filterCargoSources path type);
in
pkgs.lib.cleanSourceWith {
src = ./.;
filter = customFilter;
name = "source";
};
# Common arguments can be set here to avoid repeating them later
commonArgs = {
inherit src;
strictDeps = true;
buildInputs =
[
# Add additional build inputs here
pkgs.openssl
pkgs.pkg-config
pkgs.postgresql
]
++ lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
];
# Additional environment variables can be set directly
# MY_CUSTOM_VAR = "some value";
POSTGRES_DB = "nextrack_test";
POSTGRES_USER = "loco";
POSTGRES_PASSWORD = "loco";
DATABASE_URL = "postgres://loco:loco@localhost:5432/nextrack_test";
};
# 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.
my-crate = craneLib.buildPackage (
commonArgs
// {
doCheck = false;
inherit cargoArtifacts;
}
);
# Define the Docker image build
dockerImage = pkgs.dockerTools.buildImage {
name = "nextrack";
tag = "latest";
copyToRoot = [ my-crate ];
config = {
Cmd = [ "${my-crate}/bin/nextrack-cli" ];
};
};
postgresSetup = ''
export PGDATA=$(mktemp -d /tmp/XXXXXX)
${pkgs.postgresql}/bin/initdb "$PGDATA"
${pkgs.postgresql}/bin/pg_ctl -D "$PGDATA" --options "-c unix_socket_directories=$PGDATA" start
${pkgs.postgresql}/bin/createdb -h localhost ${commonArgs.POSTGRES_DB}
${pkgs.postgresql}/bin/psql -h localhost -d ${commonArgs.POSTGRES_DB} -c "CREATE USER ${commonArgs.POSTGRES_USER} WITH PASSWORD '${commonArgs.POSTGRES_PASSWORD}';"
${pkgs.postgresql}/bin/psql -h localhost -d ${commonArgs.POSTGRES_DB} -c "ALTER USER ${commonArgs.POSTGRES_USER} WITH SUPERUSER;"
echo "PostgreSQL started"
'';
postgresCleanup = ''
${pkgs.postgresql}/bin/pg_ctl -D "$PGDATA" stop
'';
in
{
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit my-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.
my-crate-clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
# my-crate-doc = craneLib.cargoDoc (commonArgs // {
# inherit cargoArtifacts;
# });
# Check formatting
my-crate-fmt = craneLib.cargoFmt {
inherit src;
};
# # Audit dependencies
# my-crate-audit = craneLib.cargoAudit {
# inherit src advisory-db;
# };
# # Audit licenses
# my-crate-deny = craneLib.cargoDeny {
# inherit src;
# };
# Run tests with cargo-nextest
# Consider setting `doCheck = false` on `my-crate` if you do not want
# the tests to run twice
my-crate-nextest = craneLib.cargoNextest (
commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
cargoNextestExtraArgs = " --test-threads=1";
preCheck = postgresSetup;
postCheck = postgresCleanup;
}
);
};
packages = {
default = my-crate;
inherit
my-crate
dockerImage
;
};
apps.default = flake-utils.lib.mkApp {
drv = my-crate;
};
devShells.default = craneLib.devShell {
# Inherit inputs from checks.
checks = self.checks.${system};
# Additional dev-shell environment variables can be set directly
# MY_CUSTOM_DEVELOPMENT_VAR = "something else";
# Extra inputs can be added here; cargo and rustc are provided by default.
packages = with pkgs; [
cargo-nextest
sea-orm-cli
cargo-insta
cargo-edit
nodejs
pnpm
biome
];
};
}
);
} |
I think both the blog and |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a blog post describing how to get up and running with Loco on NixOS using a nix flake providing a development shell.
In #559, multiple people appeared interested in this.
I do not have a blog, so hope it's OK that I just added a new entry in the docs blog. If a different location is more fitting, I'm happy to move it there!