add nix flake (#210)

- add following functionality:
  - nix develop with direnv support
  - nix run and nix shell
  - nix fmt for flake.nix
  - and package overlay for https://github.com/NixOS/nixpkgs/pull/225502

- useful docs
  - https://stackoverflow.com/questions/53272197/how-do-i-override-the-libc-in-a-nix-package-to-be-musl
  - dd3aca2d0b/pkgs/top-level/stage.nix (L136)

- inspired by https://github.com/typst/typst/blob/main/flake.nix
This commit is contained in:
StepBroBD 2023-06-04 09:57:28 -06:00 committed by GitHub
parent 19f79a4349
commit 49d64805ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 153 additions and 1 deletions

78
flake.nix Normal file
View file

@ -0,0 +1,78 @@
{
inputs = {
nixpkgs.url = "flake:nixpkgs/nixpkgs-unstable";
utils.url = "flake:flake-utils";
};
outputs = { self, nixpkgs, utils, ... }: utils.lib.eachSystem [
"aarch64-darwin"
"x86_64-darwin"
"aarch64-linux"
"x86_64-linux"
]
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
# enable musl on Linux makes the build time 100x slower
# since it will trigger a toolchain rebuild
# if nixpkgs.legacyPackages.${system}.stdenv.hostPlatform.isLinux
# then nixpkgs.legacyPackages.${system}.pkgsMusl
# else nixpkgs.legacyPackages.${system};
crunchy-cli = pkgs.rustPlatform.buildRustPackage.override { stdenv = pkgs.clangStdenv; } rec {
pname = "crunchy-cli";
inherit ((pkgs.lib.importTOML ./Cargo.toml).package) version;
src = pkgs.lib.cleanSource ./.;
cargoLock = {
lockFile = ./Cargo.lock;
allowBuiltinFetchGit = true;
};
nativeBuildInputs = [
pkgs.pkg-config
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.xcbuild
];
buildInputs = [
pkgs.openssl
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.Security
];
};
in
{
packages.default = crunchy-cli;
overlays.default = _: prev: {
crunchy-cli = prev.crunchy-cli.override { };
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
cargo
clippy
rust-analyzer
rustc
rustfmt
];
inputsFrom = builtins.attrValues self.packages.${system};
buildInputs = [
pkgs.openssl
pkgs.libiconv
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.apple_sdk.frameworks.CoreServices
pkgs.darwin.Security
];
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc;
};
formatter = pkgs.nixpkgs-fmt;
}
);
}