1use crate::tasks::workflows::{
  2    runners::{Arch, Platform},
  3    steps::NamedJob,
  4};
  5
  6use super::{runners, steps, steps::named, vars};
  7use gh_workflow::*;
  8use indoc::indoc;
  9
 10pub(crate) fn build_nix(
 11    platform: Platform,
 12    arch: Arch,
 13    flake_output: &str,
 14    cachix_filter: Option<&str>,
 15    deps: &[&NamedJob],
 16) -> NamedJob {
 17    let runner = match platform {
 18        Platform::Windows => unimplemented!(),
 19        Platform::Linux => runners::LINUX_X86_BUNDLER,
 20        Platform::Mac => runners::MAC_DEFAULT,
 21    };
 22    let mut job = Job::default()
 23        .timeout_minutes(60u32)
 24        .continue_on_error(true)
 25        .cond(Expression::new(
 26            "github.repository_owner == 'zed-industries'",
 27        ))
 28        .runs_on(runner)
 29        .add_env(("ZED_CLIENT_CHECKSUM_SEED", vars::ZED_CLIENT_CHECKSUM_SEED))
 30        .add_env(("ZED_MINIDUMP_ENDPOINT", vars::ZED_SENTRY_MINIDUMP_ENDPOINT))
 31        .add_env((
 32            "ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON",
 33            vars::ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON,
 34        ))
 35        .add_env(("GIT_LFS_SKIP_SMUDGE", "1")) // breaks the livekit rust sdk examples which we don't actually depend on
 36        .add_step(steps::checkout_repo());
 37
 38    if deps.len() > 0 {
 39        job = job.needs(deps.iter().map(|d| d.name.clone()).collect::<Vec<String>>());
 40    }
 41
 42    job = if platform == Platform::Linux {
 43        job.add_step(install_nix())
 44            .add_step(cachix_action(cachix_filter))
 45            .add_step(build(&flake_output))
 46    } else {
 47        job.add_step(set_path())
 48            .add_step(cachix_action(cachix_filter))
 49            .add_step(build(&flake_output))
 50            .add_step(limit_store())
 51    };
 52
 53    NamedJob {
 54        name: format!("build_nix_{platform}_{arch}"),
 55        job,
 56    }
 57}
 58
 59// on our macs we manually install nix. for some reason the cachix action is running
 60// under a non-login /bin/bash shell which doesn't source the proper script to add the
 61// nix profile to PATH, so we manually add them here
 62pub fn set_path() -> Step<Run> {
 63    named::bash(indoc! {r#"
 64            echo "/nix/var/nix/profiles/default/bin" >> "$GITHUB_PATH"
 65            echo "/Users/administrator/.nix-profile/bin" >> "$GITHUB_PATH"
 66        "#})
 67}
 68
 69pub fn install_nix() -> Step<Use> {
 70    named::uses(
 71        "cachix",
 72        "install-nix-action",
 73        "02a151ada4993995686f9ed4f1be7cfbb229e56f", // v31
 74    )
 75    .add_with(("github_access_token", vars::GITHUB_TOKEN))
 76}
 77
 78pub fn cachix_action(cachix_filter: Option<&str>) -> Step<Use> {
 79    let mut step = named::uses(
 80        "cachix",
 81        "cachix-action",
 82        "0fc020193b5a1fa3ac4575aa3a7d3aa6a35435ad", // v16
 83    )
 84    .add_with(("name", "zed"))
 85    .add_with(("authToken", vars::CACHIX_AUTH_TOKEN))
 86    .add_with(("cachixArgs", "-v"));
 87    if let Some(cachix_filter) = cachix_filter {
 88        step = step.add_with(("pushFilter", cachix_filter));
 89    }
 90    step
 91}
 92
 93pub fn build(flake_output: &str) -> Step<Run> {
 94    named::bash(&format!(
 95        "nix build .#{} -L --accept-flake-config",
 96        flake_output
 97    ))
 98}
 99
100pub fn limit_store() -> Step<Run> {
101    named::bash(indoc! {r#"
102            if [ "$(du -sm /nix/store | cut -f1)" -gt 50000 ]; then
103                nix-collect-garbage -d || true
104            fi"#
105    })
106}