Repository: railwayapp/nixpacks
Pull request #166 (2022-06-02 20:01:57)

Custom Install Cmd
* Adds `NIXPACKS_INSTALL_CMD` envvar and consumes it
* Updates tests to test for this


Patch:
diff --git a/src/main.rs b/src/main.rs
index c520b18..c5b6c5e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -61,6 +61,14 @@ fn main() -> Result<()> {
                 .takes_value(true)
                 .global(true),
         )
+        .arg(
+            Arg::new("install_cmd")
+                .long("install-cmd")
+                .short('i')
+                .help("Specify the install command to use")
+                .takes_value(true)
+                .global(true),
+        )
         .arg(
             Arg::new("build_cmd")
                 .long("build-cmd")
@@ -103,6 +111,7 @@ fn main() -> Result<()> {
         )
         .get_matches();
 
+    let install_cmd = matches.value_of("install_cmd").map(|s| s.to_string());
     let build_cmd = matches.value_of("build_cmd").map(|s| s.to_string());
     let start_cmd = matches.value_of("start_cmd").map(|s| s.to_string());
     let pkgs = match matches.values_of("pkgs") {
@@ -119,6 +128,7 @@ fn main() -> Result<()> {
     let plan_path = matches.value_of("plan").map(|n| n.to_string());
 
     let plan_options = &GeneratePlanOptions {
+        custom_install_cmd: install_cmd,
         custom_start_cmd: start_cmd,
         custom_build_cmd: build_cmd,
         custom_pkgs: pkgs,
diff --git a/src/nixpacks/plan/generator.rs b/src/nixpacks/plan/generator.rs
index d1afea0..e3d0272 100644
--- a/src/nixpacks/plan/generator.rs
+++ b/src/nixpacks/plan/generator.rs
@@ -18,6 +18,7 @@ static NIXPKGS_ARCHIVE: &str = "41cc1d5d9584103be4108c1815c350e07c807036";
 
 #[derive(Clone, Default, Debug)]
 pub struct GeneratePlanOptions {
+    pub custom_install_cmd: Option<String>,
     pub custom_build_cmd: Option<String>,
     pub custom_start_cmd: Option<String>,
     pub custom_pkgs: Vec<Pkg>,
@@ -123,11 +124,25 @@ impl<'a> NixpacksBuildPlanGenerator<'a> {
     }
 
     fn get_install_phase(&self, app: &App, environment: &Environment) -> Result<InstallPhase> {
-        let install_phase = match self.matched_provider {
+        let mut install_phase = match self.matched_provider {
             Some(provider) => provider.install(app, environment)?.unwrap_or_default(),
             None => InstallPhase::default(),
         };
 
+        let env_install_cmd = environment.get_config_variable("INSTALL_CMD").cloned();
+
+        // Start command priority
+        // - custom install command
+        // - environment variable
+        // - provider
+
+        install_phase.cmd = self
+            .options
+            .custom_install_cmd
+            .clone()
+            .or(env_install_cmd)
+            .or(install_phase.cmd);
+
         Ok(install_phase)
     }
 


Test patch:
diff --git a/tests/generate_plan_tests.rs b/tests/generate_plan_tests.rs
index f75e716..d8d5419 100644
--- a/tests/generate_plan_tests.rs
+++ b/tests/generate_plan_tests.rs
@@ -415,12 +415,14 @@ fn test_config_from_environment_variables() -> Result<()> {
         "./examples/hello",
         vec![
             "NIXPACKS_PKGS=cowsay ripgrep",
+            "NIXPACKS_INSTALL_CMD=install",
             "NIXPACKS_BUILD_CMD=build",
             "NIXPACKS_START_CMD=start",
             "NIXPACKS_RUN_IMAGE=alpine",
         ],
         &GeneratePlanOptions::default(),
     )?;
+    assert_eq!(plan.install.unwrap().cmd, Some("install".to_string()));
     assert_eq!(plan.build.unwrap().cmd, Some("build".to_string()));
     assert_eq!(plan.start.clone().unwrap().cmd, Some("start".to_string()));
     assert_eq!(