Skip to content

Commit

Permalink
Improve Nix flake
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Oct 15, 2024
1 parent 83bc1ee commit e5fdb59
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 166 deletions.
9 changes: 3 additions & 6 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ fi
# make .direnv if it doesn't exist
mkdir -p "$(direnv_layout_dir)"

watch_file shell.nix
watch_file flake.nix # as outputs can change at anytime
watch_file flake.lock

# we need to watch the rust-toolchain.toml file so if any edits occur,
# then direnv can react to it and change the environment.
watch_file rust-toolchain.toml
watch_file nix/devshell.nix
watch_file nix/packages/charted.nix
watch_file nix/packages/helm-plugin.nix

# try to use flakes, if it fails use normal nix (ie. shell.nix)
use flake || use nix
Expand Down
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components
name: Checkout repository
s

# node-waf configuration
.lock-wscript
Expand Down Expand Up @@ -692,6 +688,7 @@ FodyWeavers.xsd

!crates/helm-charts/__fixtures__/*.tgz
crates/database/data.db
!nix/packages/*.nix
!hack/release/
!crates/bin/
!Cargo.lock
Expand Down
22 changes: 2 additions & 20 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

157 changes: 23 additions & 134 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
{
description = "charted is a free, open, and reliable way to distribute Helm charts";
description = "🐻‍❄️📦 Free, open-source way to distribute Helm charts across the world";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
systems.url = "github:nix-systems/default";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs = {
Expand All @@ -32,143 +32,32 @@

outputs = {
nixpkgs,
flake-utils,
rust-overlay,
systems,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};

cargoTOML = builtins.fromTOML (builtins.readFile ./Cargo.toml);
rust = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
rustflags =
if pkgs.stdenv.isLinux
then ''-C link-arg=-fuse-ld=mold -C target-cpu=native $RUSTFLAGS''
else ''$RUSTFLAGS'';

# We use Rust nightly for all crates, so we want to match it.
rustPlatform = pkgs.makeRustPlatform {
cargo = rust;
rustc = rust;
};

charted = rustPlatform.buildRustPackage {
nativeBuildInputs = with pkgs; [pkg-config installShellFiles];
buildInputs = with pkgs;
[openssl sqlite postgresql]
++ (lib.optional stdenv.isDarwin (with darwin.apple_sdk.frameworks; [CoreFoundation Security SystemConfiguration]));

version = "${cargoTOML.workspace.package.version}";
name = "charted";
src = ./.;

env.CHARTED_DISTRIBUTION_KIND = "nix";
cargoBuildFlags = ["--package" "charted"];
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"azalia-0.1.0" = "sha256-b0C26qCYQ1aLqgbVXwM+j8WCJxKA19a3lMfveVxSrFs=";
};
};

postInstall = ''
installShellCompletion --cmd charted \
--bash <($out/bin/charted completions bash) \
--fish <($out/bin/charted completions fish) \
--zsh <($out/bin/charted completions zsh)
'';

useNextest = true;
meta = with pkgs.lib; {
description = "charted is a free, open, and reliable way to distribute Helm charts";
homepage = "https://charts.noelware.org";
license = with licenses; [asl20];
maintainers = with maintainers; [auguwu spotlightishere noelware];
mainProgram = "charted";
};
};

helm-plugin = rustPlatform.buildRustPackage {
nativeBuildInputs = with pkgs; [pkg-config protobuf];
buildInputs = with pkgs;
[openssl sqlite postgresql]
++ (lib.optional stdenv.isDarwin (with darwin.apple_sdk.frameworks; [CoreFoundation Security SystemConfiguration]));
cargoSha256 = pkgs.lib.fakeSha256;
version = "${cargoTOML.workspace.package.version}";
name = "charted-helm-plugin";
src = ./.;

env.CHARTED_DISTRIBUTION_KIND = "nix";

cargoBuildFlags = ["--package" "charted-helm-plugin"];
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"azalia-0.1.0" = "sha256-b0C26qCYQ1aLqgbVXwM+j8WCJxKA19a3lMfveVxSrFs=";
};
};

postPatch = ''
sed -i '/^hooks:/,+2 d' plugin.yaml
'';

postInstall = ''
install -Dm644 plugin.yaml $out/charted-helm-plugin/plugin.yaml
mv $out/bin $out/charted-helm-plugin
'';

useNextest = true;
meta = with pkgs.lib; {
description = "Helm plugin to help aid developing Helm charts with charted";
homepage = "https://charts.noelware.org";
license = with licenses; [asl20];
maintainers = with maintainers; [auguwu spotlightishere noelware];
};
}: let
eachSystem = nixpkgs.lib.genAttrs (import systems);
overlays = [(import rust-overlay)];
nixpkgsFor = system:
import nixpkgs {
inherit system overlays;
};
in {
formatter = eachSystem (system: (nixpkgsFor system).alejandra);
packages = eachSystem (system: let
pkgs = nixpkgsFor system;
charted = pkgs.callPackage ./nix/packages/charted.nix {};
helm-plugin = pkgs.callPackage ./nix/packages/helm-plugin.nix {};
in {
packages = {
inherit charted helm-plugin;

default = charted;
all = pkgs.symlinkJoin {
name = "charted-${cargoTOML.workspace.package.version}";
paths = [charted helm-plugin];
};
};

devShells.default = pkgs.mkShell {
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (with pkgs; [openssl sqlite postgresql]);
nativeBuildInputs = with pkgs;
[pkg-config sqlite postgresql.lib]
++ (lib.optional stdenv.isLinux [mold lldb])
++ (lib.optional stdenv.isDarwin (with darwin.apple_sdk.frameworks; [CoreFoundation Security SystemConfiguration]));

buildInputs = with pkgs;
[
cargo-nextest
cargo-machete
cargo-deny
inherit charted helm-plugin;

# I don't plan to add MySQL support and probably never will.
(diesel-cli.override {
mysqlSupport = false;
postgresqlSupport = true;
sqliteSupport = true;
})

openssl
git

rust
]
++ (lib.optional stdenv.isLinux [glibc]);
default = charted;
});

shellHook = ''
export RUSTFLAGS="--cfg tokio_unstable ${rustflags}"
'';
};
devShells = eachSystem (system: let
pkgs = nixpkgsFor system;
in {
default = import ./nix/devshell.nix {inherit pkgs;};
});
};
}
32 changes: 32 additions & 0 deletions nix/common.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 🐻‍❄️📦 charted-server: Free, open source, and reliable Helm Chart registry made in Rust
# Copyright 2022-2024 Noelware, LLC. <team@noelware.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
{
cargoTOML = builtins.fromTOML (builtins.readFile ../Cargo.toml);
outputHashes = {
"azalia-0.1.0" = "sha256-b0C26qCYQ1aLqgbVXwM+j8WCJxKA19a3lMfveVxSrFs=";
};

rustflags = stdenv:
if stdenv.isLinux
then ''-C link-arg=-fuse-ld=mold -C target-cpu=native $RUSTFLAGS''
else ''$RUSTFLAGS'';

mkRustPlatform = rust: rust.fromRustupToolchainFile ../rust-toolchain.toml;
mkNixpkgsRustPlatform = pkgs: toolchain:
pkgs.makeRustPlatform {
rustc = toolchain;
cargo = toolchain;
};
}
54 changes: 54 additions & 0 deletions nix/devshell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 🐻‍❄️📦 charted-server: Free, open source, and reliable Helm Chart registry made in Rust
# Copyright 2022-2024 Noelware, LLC. <team@noelware.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
{pkgs}:
with pkgs; let
common = import ./common.nix;
toolchain = common.mkRustPlatform pkgs.rust-bin;
rustflags = common.rustflags stdenv;
in
mkShell {
LD_LIBRARY_PATH = lib.makeLibraryPath [openssl sqlite postgresql];
nativeBuildInputs =
[pkg-config sqlite postgresql.lib]
++ (lib.optional stdenv.isLinux [mold lldb])
++ (lib.optional stdenv.isDarwin (with darwin.apple_sdk.frameworks; [
CoreFoundation
Security
SystemConfiguration
]));

buildInputs =
[
cargo-nextest # replacement for `cargo test`
cargo-machete # used to validate dependencies
cargo-deny # used to validate licenses, vulns, etc.

# I don't plan to add MySQL support and probably never will.
(diesel-cli.override {
mysqlSupport = false;
postgresqlSupport = true;
sqliteSupport = true;
})

toolchain # rust toolchain
openssl
git
]
++ (lib.optional stdenv.isLinux [glibc]);

shellHook = ''
export RUSTFLAGS="--cfg tokio_unstable ${rustflags}"
'';
}
Loading

0 comments on commit e5fdb59

Please sign in to comment.