snix/snix/utils.nix
Marijan Petričević 6b48bcc1bf feat(snix/castore-http): initial implementation
The castore-http crate provides both a binary and a library interface to
serve a single castore root node over HTTP.

The library function `get_root_node_contents` will return a
`axum::Response` for a requested path in the castore root node
depending on the requested paths type.

If the requested path in the root node is a directory, we return:
- a index file if there is a file matching one of the configurable
  `index_names`
- a directory listing, if no `index_names` were configured and
  `auto_index` was enabled
- the FORBIDDEN status code if no `index_names` were set nor
  `auto_index` was enabled

If the requested path in the root node is a file,
we return the file.

If the requested path in the root node is a symlink,
we figure out wether the target exists and return a REDIRECT.

If the requested path doesn't exist in the root node,
we respond with NOT_FOUND

The binary wraps this functionality and allows one to specify the
desired root node by providing its base-64 encoded representation as
well as the other configuration parameters affecting the behavior of
`get_root_node_contents`.

Change-Id: I737482299f788ec0244c54b52042f9eb655a05c2
Reviewed-on: https://cl.snix.dev/c/snix/+/30245
Autosubmit: Marijan Petričević <marijan.petricevic94@gmail.com>
Reviewed-by: Marijan Petričević <marijan.petricevic94@gmail.com>
Tested-by: besadii
Reviewed-by: Florian Klink <flokli@flokli.de>
2025-04-14 14:43:25 +00:00

163 lines
5.6 KiB
Nix

{ pkgs, lib, depot, ... }:
let
# Filters the given source, only keeping files related to the build, preventing unnecessary rebuilds.
# Includes src in the root, all other .rs files and optionally Cargo specific files.
# Additional files to be included can be specified in extraFileset.
filterRustCrateSrc =
{ root # The original src
, extraFileset ? null # Additional filesets to include (e.g. fileFilter for proto files)
, cargoSupport ? false
}:
lib.fileset.toSource {
inherit root;
fileset = lib.fileset.intersection
(lib.fileset.fromSource root) # We build our final fileset from the original src
(lib.fileset.unions ([
(lib.fileset.maybeMissing (root + "/src")) # src may be missing if the crate just has tests for example
(lib.fileset.fileFilter (f: f.hasExt "rs") root)
] ++ lib.optionals cargoSupport [
(lib.fileset.fileFilter (f: f.name == "Cargo.toml") root)
(lib.fileset.maybeMissing (root + "/Cargo.lock"))
] ++ lib.optional (extraFileset != null) extraFileset));
};
in
{
mkFeaturePowerset = { crateName, features, override ? { } }:
let
powerset = xs:
let
addElement = set: element:
set ++ map (e: [ element ] ++ e) set;
in
lib.foldl' addElement [ [ ] ] xs;
in
lib.listToAttrs (map
(featuresPowerset: {
name = if featuresPowerset != [ ] then "with-features-${lib.concatStringsSep "-" featuresPowerset}" else "no-features";
value = depot.snix.crates.workspaceMembers.${crateName}.build.override (old: {
runTests = true;
features = featuresPowerset;
} // (if lib.isFunction override then override old else override)
);
})
(powerset features));
inherit filterRustCrateSrc;
# A function which takes a pkgs instance and returns an overriden defaultCrateOverrides with support for snix crates.
# This can be used throughout the rest of the repo.
defaultCrateOverridesForPkgs = pkgs:
pkgs.defaultCrateOverrides // {
nar-bridge = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
nix-compat = prev: {
src = filterRustCrateSrc rec {
root = prev.src.origSrc;
extraFileset = root + "/testdata";
};
};
nix-compat-derive = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
nix-compat-derive-tests = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
nix-daemon = prev: {
src = depot.snix.utils.filterRustCrateSrc { root = prev.src.origSrc; };
};
snix-build = prev: {
src = filterRustCrateSrc rec {
root = prev.src.origSrc;
extraFileset = lib.fileset.fileFilter (f: f.hasExt "proto") root;
};
PROTO_ROOT = depot.snix.build.protos.protos;
nativeBuildInputs = [ pkgs.protobuf ];
SNIX_BUILD_SANDBOX_SHELL = if pkgs.stdenv.isLinux then pkgs.pkgsStatic.busybox + "/bin/sh" else "/bin/sh";
};
snix-castore = prev: {
src = filterRustCrateSrc rec {
root = prev.src.origSrc;
extraFileset = lib.fileset.fileFilter (f: f.hasExt "proto") root;
};
PROTO_ROOT = depot.snix.castore.protos.protos;
nativeBuildInputs = [ pkgs.protobuf ];
};
snix-castore-htp = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
snix-cli = prev: {
src = filterRustCrateSrc rec {
root = prev.src.origSrc;
extraFileset = root + "/tests";
};
};
snix-store = prev: {
src = filterRustCrateSrc rec {
root = prev.src.origSrc;
extraFileset = lib.fileset.fileFilter (f: f.hasExt "proto") root;
};
PROTO_ROOT = depot.snix.store.protos.protos;
nativeBuildInputs = [ pkgs.protobuf ];
};
snix-eval-builtin-macros = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
snix-eval = prev: {
src = filterRustCrateSrc rec {
root = prev.src.origSrc;
extraFileset = root + "/proptest-regressions";
};
};
snix-glue = prev: {
src = filterRustCrateSrc {
root = prev.src.origSrc;
};
};
snix-serde = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
snix-tracing = prev: {
src = filterRustCrateSrc { root = prev.src.origSrc; };
};
};
# This creates an extraStep in CI to check whether the Cargo.nix file is up-to-date.
mkCrate2nixCheck =
path: # The path to the Cargo.nix to be checked.
let
relCrateRoot = lib.removePrefix "./" (builtins.dirOf (lib.path.removePrefix depot.path.origSrc path));
in
{
label = "crate2nix check for ${relCrateRoot}";
needsOutput = true;
alwaysRun = true;
command = pkgs.writeShellScript "crate2nix-check-for-${lib.replaceStrings [ "/" ] ["-"] relCrateRoot}" ''
(cd $(git rev-parse --show-toplevel)/${relCrateRoot} &&
${depot.tools.crate2nix-generate}/bin/crate2nix-generate &&
if [[ -n "$(git status --porcelain -unormal Cargo.nix)" ]]; then
echo "----------------------------------------------------------------------------------------------------"
echo "Cargo.nix needs to be updated, run 'mg run //tools/crate2nix-generate' in ${relCrateRoot}"
echo "----------------------------------------------------------------------------------------------------"
exit 1
fi
)
'';
};
}