2023-02-23 07:47:24 +03:00
|
|
|
## Local Development
|
|
|
|
|
|
|
|
[Nix shells (or development
|
|
|
|
shells)](https://nix.dev/tutorials/ad-hoc-developer-environments) are extremely
|
|
|
|
powerful when it comes to locally developing with the exact same dependencies
|
|
|
|
used when building packages.
|
|
|
|
|
2023-09-03 20:33:10 +03:00
|
|
|
To get started, declare a default `devShell` in `flake.nix` using
|
|
|
|
[`craneLib.devShell`](API.md#cranelibdevshell) and run `nix develop` in the
|
|
|
|
project directory. Then, you can use something like
|
2023-02-23 07:47:24 +03:00
|
|
|
[`direnv`](https://direnv.net) or
|
|
|
|
[`nix-direnv`](https://github.com/nix-community/nix-direnv) to automatically
|
2023-09-03 20:33:10 +03:00
|
|
|
enter and exit a development shell when you enter or exit the project
|
|
|
|
directory!
|
2023-02-23 07:47:24 +03:00
|
|
|
|
|
|
|
Sample `flake.nix`:
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
inputs = {
|
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
|
|
|
|
2024-08-30 02:29:16 +03:00
|
|
|
crane.url = "github:ipetkov/crane";
|
2023-02-23 07:47:24 +03:00
|
|
|
|
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
|
|
|
};
|
|
|
|
|
|
|
|
outputs = { self, nixpkgs, crane, flake-utils, ... }:
|
|
|
|
flake-utils.lib.eachDefaultSystem (system:
|
|
|
|
let
|
2024-05-19 04:07:07 +03:00
|
|
|
pkgs = nixpkgs.legacyPackages.${system};
|
|
|
|
craneLib = crane.mkLib pkgs;
|
2023-02-23 07:47:24 +03:00
|
|
|
|
|
|
|
my-crate = craneLib.buildPackage {
|
2024-06-11 06:53:46 +03:00
|
|
|
src = craneLib.cleanCargoSource ./.;
|
2023-02-23 07:47:24 +03:00
|
|
|
|
|
|
|
buildInputs = [
|
|
|
|
# Add additional build inputs here
|
|
|
|
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
|
|
|
|
# Additional darwin specific inputs can be set here
|
|
|
|
pkgs.libiconv
|
|
|
|
];
|
|
|
|
|
|
|
|
# Additional environment variables can be set directly
|
|
|
|
# MY_CUSTOM_VAR = "some value";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
packages.default = my-crate;
|
|
|
|
|
2023-09-03 20:33:10 +03:00
|
|
|
devShells.default = craneLib.devShell {
|
2023-02-23 07:47:24 +03:00
|
|
|
# Additional dev-shell environment variables can be set directly
|
|
|
|
MY_CUSTOM_DEV_URL = "http://localhost:3000";
|
|
|
|
|
|
|
|
# Automatically inherit any build inputs from `my-crate`
|
|
|
|
inputsFrom = [ my-crate ];
|
|
|
|
|
|
|
|
# Extra inputs (only used for interactive development)
|
2023-09-03 20:33:10 +03:00
|
|
|
# can be added here; cargo and rustc are provided by default.
|
|
|
|
packages = [
|
|
|
|
pkgs.cargo-audit
|
|
|
|
pkgs.cargo-watch
|
2023-02-23 07:47:24 +03:00
|
|
|
];
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Then, after integrating direnv into your shell:
|
|
|
|
```sh
|
|
|
|
echo "use flake" > .envrc
|
|
|
|
direnv allow
|
|
|
|
```
|