Skip to content

Commit

Permalink
Orca: Use crane in nix build (#130)
Browse files Browse the repository at this point in the history
Improve incrementality of nix build for orca using crane. This
introduces a breaking change in `default.nix` which will require action
in all downstream dependencies relying on `default.nix`
  • Loading branch information
turboMaCk authored and ICTGuerrilla committed Oct 21, 2023
1 parent ba546b9 commit 3813cce
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 41 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/orca.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:
jobs:
nix-build:
runs-on: ubuntu-latest
# if: github.ref_name == 'main'
steps:
- name: Clone repository
uses: actions/checkout@v3
Expand All @@ -15,6 +14,10 @@ jobs:
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main

- name: Check orca
working-directory: orca
run: nix flake check --log-format raw

- name: Build via nix
working-directory: orca
run: nix build
run: nix build --log-format raw
62 changes: 44 additions & 18 deletions orca/default.nix
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
{ rustPlatform
, pkg-config
{ pkg-config
, openssl
, stdenv
, darwin
, lib
, callPackage
, texlive
, makeWrapper
, nix-gitignore
, ibm-plex
, buildFeatures ? []
, crane
, system
, lib
, darwin
}:
let
tex = import ./latex { inherit texlive; };
in
rustPlatform.buildRustPackage {
inherit buildFeatures;

pname = "ict-union-orca";
version = "0.1.0";
src = nix-gitignore.gitignoreSource [] ./.;
cargoSha256 = "sha256-iW18bgQwXIT6jL+PW3UxELqUzDJLxldAwwJv2FudB4o=";
craneLib = crane.lib.${system};
nativeBuildInputs = [
openssl
pkg-config
];

nativeBuildInputs = [ pkg-config makeWrapper ];
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly {
inherit src nativeBuildInputs;
};

buildInputs = [ openssl ] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
];
postInstall = ''
wrapProgram "$out/bin/orca" --suffix PATH : "${tex}/bin"
# Run clippy (and deny all warnings) on the crate source,
# resuing the dependency artifacts (e.g. from build scripts or
# proc-macros) from above.
#
# Note that this is done as a separate derivation so it
# does not impact building just the crate by itself.
orca-clippy = craneLib.cargoClippy {
inherit cargoArtifacts src nativeBuildInputs;
cargoClippyExtraArgs = "-- --deny warnings";
};

orca-fmt = craneLib.cargoFmt {
inherit src;
};
in
rec {
orca = craneLib.buildPackage {
inherit cargoArtifacts src;
nativeBuildInputs = nativeBuildInputs ++ [ makeWrapper ];
buildInputs = [ openssl ] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
];
postInstall = ''
wrapProgram "$out/bin/orca" --suffix PATH : "${tex}/bin"
'';
};

inherit orca-clippy;
inherit orca-fmt;
}
51 changes: 45 additions & 6 deletions orca/flake.lock

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

21 changes: 16 additions & 5 deletions orca/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
inputs = {
nixpkgs.url = github:NixOS/nixpkgs;
flake-utils.url = github:numtide/flake-utils;
crane.url = "github:ipetkov/crane";
crane.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system:
outputs = { self, nixpkgs, flake-utils, crane }: flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
Expand All @@ -18,9 +20,13 @@
openssl
pkg-config
];
in

orcaPkgs = pkgs.callPackage ./default.nix {
inherit crane;
};
in rec
{
devShell = with pkgs;
devShells.default = with pkgs;
mkShell {
name = "ict-union-orca-dev-env";
inherit buildInputs;
Expand All @@ -30,6 +36,11 @@
'';
OSFONTDIR = "${pkgs.ibm-plex}/share/fonts/opentype";
};
defaultPackage = pkgs.callPackage ./default.nix {};
});

defaultPackage = orcaPkgs.orca;

checks = {
inherit (orcaPkgs) orca orca-clippy orca-fmt;
};
});
}
8 changes: 6 additions & 2 deletions orca/src/api/applications/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,12 @@ async fn hard_delete<'r>(
.to_status()
.assert_rejected()?;

query::dangerous_hard_delete_application_data(id).execute(&mut tx).await?;
query::dangerous_hard_delete_application(id).execute(&mut tx).await?;
query::dangerous_hard_delete_application_data(id)
.execute(&mut tx)
.await?;
query::dangerous_hard_delete_application(id)
.execute(&mut tx)
.await?;

tx.commit().await?;

Expand Down
23 changes: 15 additions & 8 deletions orca/src/processing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async fn process(

match command {
NewRegistrationRequest(reg_id, signature) => {
process_new_registration(reg_id, signature, config, db_pool).await
process_new_registration(reg_id, signature, config, db_pool).await?;
}
ResentRegistrationEmail(reg_id) => {
let application_details = query::query_registration(reg_id).fetch_one(db_pool).await?;
Expand All @@ -145,7 +145,7 @@ async fn process(
.fetch_one(db_pool)
.await?;

send_verification_email(config, db_pool, &application_details, pdf_data).await
send_verification_email(config, db_pool, &application_details, pdf_data).await?;
}
RegistrationRequestVerified(reg_id) => {
// We do this only if notification email is configured
Expand Down Expand Up @@ -200,10 +200,10 @@ async fn process(

send_email(config, message).await?;
}

Ok(())
}
}

Ok(())
}

async fn send_email(config: &Config, message: Message) -> Result<(), ProcessingError> {
Expand Down Expand Up @@ -231,8 +231,8 @@ async fn process_new_registration(
.await?;

// Query for detail information about member
// in theory we could also pass this in thee command
// but doing it this way means that all triggers, defaults etc are 100% applied to the data
// in theory we could also pass this in the command
// but doing it this way means that all triggers & defaults etc are 100% applied to the data
let application_details = query::query_registration(reg_id).fetch_one(db_pool).await?;

// Generate PDF
Expand All @@ -256,6 +256,8 @@ async fn send_verification_email(
application_details: &RegistrationDetails,
pdf_data: Vec<u8>,
) -> Result<(), ProcessingError> {
info!("Send verification email to {}", application_details.email);

let token = application_details
.confirmation_token
.as_ref()
Expand Down Expand Up @@ -395,6 +397,8 @@ async fn print_pdf(
application_details: &RegistrationDetails,
dir: &str,
) -> Result<String, ProcessingError> {
info!("Printing registration pdf at {dir}");

// inline static files
let form_tex = include_str!("../../latex/registration.tex");
let logo_png = include_bytes!("../../latex/logo.png");
Expand Down Expand Up @@ -424,9 +428,12 @@ async fn print_pdf(
.stdout(Stdio::null())
.spawn()?;

// Await until the command completes
// Await until command completes
// There is a problem with tokio detecting the exist status of the process
// at least in cases where xelatex fails to find font in OSFONTDIR
// like similar to https://users.rust-lang.org/t/tokio-child-wait-never-returning/96657
let status = child.wait().await?;
info!("tex command exited successfully: {status}");
info!("Tex command exited successfully: {status}");

Ok(format!("{dir}/registration.pdf"))
}

0 comments on commit 3813cce

Please sign in to comment.