Skip to content

Commit

Permalink
treefmt: format nix
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen committed Jul 5, 2024
1 parent 929035c commit 89dd79f
Show file tree
Hide file tree
Showing 35 changed files with 1,007 additions and 724 deletions.
129 changes: 67 additions & 62 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,83 @@
};

outputs =
{ self
, nixpkgs
, flake-utils
, treefmt-nix
, ...
}: flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import ./overlays/nixpkgs.nix) ];
};
inherit (pkgs) lib;
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
ourPkgs = import ./packages { inherit pkgs lib; };
in
{
devShells = {
default = pkgs.mkShell {
packages = with pkgs; [
azure-cli
crane
delve
go
golangci-lint
gopls
gotools
just
kubectl
];
shellHook = ''
alias make=just
export DO_NOT_TRACK=1
'';
self,
nixpkgs,
flake-utils,
treefmt-nix,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import ./overlays/nixpkgs.nix) ];
};
docs = pkgs.mkShell {
packages = with pkgs; [
yarn
];
shellHook = ''
yarn install
'';
};
} // (
let
toDemoShell = version: contrast-release:
lib.nameValuePair "demo-${version}" (pkgs.mkShell {
packages = [ contrast-release ];
inherit (pkgs) lib;
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
ourPkgs = import ./packages { inherit pkgs lib; };
in
{
devShells =
{
default = pkgs.mkShell {
packages = with pkgs; [
azure-cli
crane
delve
go
golangci-lint
gopls
gotools
just
kubectl
];
shellHook = ''
cd "$(mktemp -d)"
[[ -e ${contrast-release}/runtime.yml ]] && install -m644 ${contrast-release}/runtime.yml .
[[ -e ${contrast-release}/coordinator.yml ]] && install -m644 ${contrast-release}/coordinator.yml .
[[ -d ${contrast-release}/deployment ]] && install -m644 -Dt ./deployment ${contrast-release}/deployment/*
alias make=just
export DO_NOT_TRACK=1
'';
});
in
lib.mapAttrs' toDemoShell ourPkgs.contrast-releases
);
};
docs = pkgs.mkShell {
packages = with pkgs; [ yarn ];
shellHook = ''
yarn install
'';
};
}
// (
let
toDemoShell =
version: contrast-release:
lib.nameValuePair "demo-${version}" (
pkgs.mkShell {
packages = [ contrast-release ];
shellHook = ''
cd "$(mktemp -d)"
[[ -e ${contrast-release}/runtime.yml ]] && install -m644 ${contrast-release}/runtime.yml .
[[ -e ${contrast-release}/coordinator.yml ]] && install -m644 ${contrast-release}/coordinator.yml .
[[ -d ${contrast-release}/deployment ]] && install -m644 -Dt ./deployment ${contrast-release}/deployment/*
export DO_NOT_TRACK=1
'';
}
);
in
lib.mapAttrs' toDemoShell ourPkgs.contrast-releases
);

formatter = treefmtEval.config.build.wrapper;
formatter = treefmtEval.config.build.wrapper;

checks = {
formatting = treefmtEval.config.build.check self;
};
checks = {
formatting = treefmtEval.config.build.check self;
};

legacyPackages = pkgs // ourPkgs;
});
legacyPackages = pkgs // ourPkgs;
}
);

nixConfig = {
extra-substituters = [
"https://edgelesssys.cachix.org"
];
extra-substituters = [ "https://edgelesssys.cachix.org" ];
extra-trusted-public-keys = [
"edgelesssys.cachix.org-1:erQG/S1DxpvJ4zuEFvjWLx/4vujoKxAJke6lK2tWeB0="
];
Expand Down
20 changes: 10 additions & 10 deletions overlays/nixpkgs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ final: prev: {
# hash = "";
# };
# });
go_1_22 = prev.go_1_22.overrideAttrs (finalAttrs: _prevAttrs: {
version = "1.22.4";
src = final.fetchurl {
url = "https://go.dev/dl/go${finalAttrs.version}.src.tar.gz";
hash = "sha256-/tcgZ45yinyjC6jR3tHKr+J9FgKPqwIyuLqOIgCPt4Q=";
};
});
go_1_22 = prev.go_1_22.overrideAttrs (
finalAttrs: _prevAttrs: {
version = "1.22.4";
src = final.fetchurl {
url = "https://go.dev/dl/go${finalAttrs.version}.src.tar.gz";
hash = "sha256-/tcgZ45yinyjC6jR3tHKr+J9FgKPqwIyuLqOIgCPt4Q=";
};
}
);

# Add the required extensions to the Azure CLI.
azure-cli = prev.azure-cli.override {
withExtensions = with final.azure-cli.extensions; [
aks-preview
];
withExtensions = with final.azure-cli.extensions; [ aks-preview ];
};
}
131 changes: 68 additions & 63 deletions packages/by-name/buildGoTest/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,84 @@
args':

let
args = args' // { "doCheck" = false; };
args = args' // {
"doCheck" = false;
};
in
buildGoModule ({
# copy of buildGoModule.buildPhase with the following changes:
# - use `go test -c -o $GOPATH/bin/` instead of `go install` to build the binary of a test package
# original:
# https://github.com/NixOS/nixpkgs/blob/c44815411ae47dd8bbbb92d60c3a83abff28a9f3/pkgs/build-support/go/module.nix#L188-L266
buildPhase = ''
runHook preBuild
buildGoModule (
{
# copy of buildGoModule.buildPhase with the following changes:
# - use `go test -c -o $GOPATH/bin/` instead of `go install` to build the binary of a test package
# original:
# https://github.com/NixOS/nixpkgs/blob/c44815411ae47dd8bbbb92d60c3a83abff28a9f3/pkgs/build-support/go/module.nix#L188-L266
buildPhase = ''
runHook preBuild
exclude='\(/_\|examples\|Godeps\|testdata'
if [[ -n "$excludedPackages" ]]; then
IFS=' ' read -r -a excludedArr <<<$excludedPackages
printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}"
excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf
exclude+='\|'"$excludedAlternates"
fi
exclude+='\)'
exclude='\(/_\|examples\|Godeps\|testdata'
if [[ -n "$excludedPackages" ]]; then
IFS=' ' read -r -a excludedArr <<<$excludedPackages
printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}"
excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf
exclude+='\|'"$excludedAlternates"
fi
exclude+='\)'
buildGoDir() {
local cmd="$1" dir="$2"
buildGoDir() {
local cmd="$1" dir="$2"
. $TMPDIR/buildFlagsArray
. $TMPDIR/buildFlagsArray
declare -a flags
flags+=($buildFlags "''${buildFlagsArray[@]}")
flags+=(''${tags:+-tags=''${tags// /,}})
flags+=(''${ldflags:+-ldflags="$ldflags"})
flags+=("-p" "$NIX_BUILD_CORES")
declare -a flags
flags+=($buildFlags "''${buildFlagsArray[@]}")
flags+=(''${tags:+-tags=''${tags// /,}})
flags+=(''${ldflags:+-ldflags="$ldflags"})
flags+=("-p" "$NIX_BUILD_CORES")
if [ "$cmd" = "test" ]; then
flags+=(-vet=off)
flags+=($checkFlags)
fi
if [ "$cmd" = "test" ]; then
flags+=(-vet=off)
flags+=($checkFlags)
fi
local OUT
if ! OUT="$(go $cmd -c -o $GOPATH/bin/ "''${flags[@]}" $dir 2>&1)"; then
if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
local OUT
if ! OUT="$(go $cmd -c -o $GOPATH/bin/ "''${flags[@]}" $dir 2>&1)"; then
if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
echo "$OUT" >&2
return 1
fi
fi
if [ -n "$OUT" ]; then
echo "$OUT" >&2
return 1
fi
return 0
}
getGoDirs() {
local type;
type="$1"
if [ -n "$subPackages" ]; then
echo "$subPackages" | sed "s,\(^\| \),\1./,g"
else
find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique | grep -v "$exclude"
fi
}
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
buildFlagsArray+=(-x)
fi
if [ -n "$OUT" ]; then
echo "$OUT" >&2
fi
return 0
}
getGoDirs() {
local type;
type="$1"
if [ -n "$subPackages" ]; then
echo "$subPackages" | sed "s,\(^\| \),\1./,g"
if [ ''${#buildFlagsArray[@]} -ne 0 ]; then
declare -p buildFlagsArray > $TMPDIR/buildFlagsArray
else
find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique | grep -v "$exclude"
touch $TMPDIR/buildFlagsArray
fi
}
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
buildFlagsArray+=(-x)
fi
if [ ''${#buildFlagsArray[@]} -ne 0 ]; then
declare -p buildFlagsArray > $TMPDIR/buildFlagsArray
else
touch $TMPDIR/buildFlagsArray
fi
if [ -z "$enableParallelBuilding" ]; then
export NIX_BUILD_CORES=1
fi
for pkg in $(getGoDirs ""); do
echo "Building subPackage $pkg"
buildGoDir test "$pkg"
done
'';
} // args)
if [ -z "$enableParallelBuilding" ]; then
export NIX_BUILD_CORES=1
fi
for pkg in $(getGoDirs ""); do
echo "Building subPackage $pkg"
buildGoDir test "$pkg"
done
'';
}
// args
)
31 changes: 17 additions & 14 deletions packages/by-name/cli-release/package.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Copyright 2024 Edgeless Systems GmbH
# SPDX-License-Identifier: AGPL-3.0-only

{ lib
, contrast
, microsoft
, genpolicy ? microsoft.genpolicy
{
lib,
contrast,
microsoft,
genpolicy ? microsoft.genpolicy,
}:

(contrast.overrideAttrs (_finalAttrs: previousAttrs: {
prePatch = ''
install -D ${lib.getExe genpolicy} cli/cmd/assets/genpolicy
install -D ${contrast.settings}/genpolicy-settings.json cli/cmd/assets/genpolicy-settings.json
install -D ${contrast.rules}/genpolicy-rules.rego cli/cmd/assets/genpolicy-rules.rego
'';
(contrast.overrideAttrs (
_finalAttrs: previousAttrs: {
prePatch = ''
install -D ${lib.getExe genpolicy} cli/cmd/assets/genpolicy
install -D ${contrast.settings}/genpolicy-settings.json cli/cmd/assets/genpolicy-settings.json
install -D ${contrast.rules}/genpolicy-rules.rego cli/cmd/assets/genpolicy-rules.rego
'';

ldflags = previousAttrs.ldflags ++ [
"-X github.com/edgelesssys/contrast/cli/cmd.DefaultCoordinatorPolicyHash=${builtins.readFile ../../../cli/cmd/assets/coordinator-policy-hash}"
];
})).cli
ldflags = previousAttrs.ldflags ++ [
"-X github.com/edgelesssys/contrast/cli/cmd.DefaultCoordinatorPolicyHash=${builtins.readFile ../../../cli/cmd/assets/coordinator-policy-hash}"
];
}
)).cli
Loading

0 comments on commit 89dd79f

Please sign in to comment.