From 55903a2f8ed80146547744aeb72130466c89d975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sat, 21 Sep 2024 16:30:50 +0200 Subject: [PATCH] writers: add babashka --- pkgs/build-support/writers/scripts.nix | 83 ++++++++++++++++++++++++++ pkgs/build-support/writers/test.nix | 40 +++++++++++++ 2 files changed, 123 insertions(+) diff --git a/pkgs/build-support/writers/scripts.nix b/pkgs/build-support/writers/scripts.nix index 81a88391f324..297996ca899c 100644 --- a/pkgs/build-support/writers/scripts.nix +++ b/pkgs/build-support/writers/scripts.nix @@ -522,6 +522,89 @@ rec { */ writeFishBin = name: writeFish "/bin/${name}"; + /** + Like writeScript but the first line is a shebang to babashka + + Can be called with or without extra arguments. + + :::{.example} + ## `pkgs.writers.writeBabashka` without arguments + + ```nix + writeBabashka "example" '' + (println "hello world") + '' + ``` + ::: + + :::{.example} + ## `pkgs.writers.writeBabashka` with arguments + + ```nix + writeBabashka "example" + { + makeWrapperArgs = [ + "--prefix" "PATH" ":" "${lib.makeBinPath [ pkgs.hello ]}" + ]; + } + '' + (require '[babashka.tasks :as tasks]) + (tasks/shell "hello" "-g" "Hello babashka!") + '' + ``` + ::: + */ + writeBabashka = + name: argsOrScript: + if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript then + makeScriptWriter ( + argsOrScript + // { + interpreter = "${lib.getExe pkgs.babashka}"; + check = "${lib.getExe pkgs.clj-kondo} --lint"; + } + ) name + else + makeScriptWriter { + interpreter = "${lib.getExe pkgs.babashka}"; + check = "${lib.getExe pkgs.clj-kondo} --lint"; + } name argsOrScript; + + /** + Like writeScriptBin but the first line is a shebang to babashka + + Can be called with or without extra arguments. + + # Examples + :::{.example} + ## `pkgs.writers.writeBabashkaBin` without arguments + + ```nix + writeBabashkaBin "example" '' + (println "hello world") + '' + ``` + ::: + + :::{.example} + ## `pkgs.writers.writeBabashkaBin` with arguments + + ```nix + writeBabashkaBin "example" + { + makeWrapperArgs = [ + "--prefix" "PATH" ":" "${lib.makeBinPath [ pkgs.hello ]}" + ]; + } + '' + (require '[babashka.tasks :as tasks]) + (tasks/shell "hello" "-g" "Hello babashka!") + '' + ``` + ::: + */ + writeBabashkaBin = name: writeBabashka "/bin/${name}"; + /** writeHaskell takes a name, an attrset with libraries and haskell version (both optional) and some haskell source code and returns an executable. diff --git a/pkgs/build-support/writers/test.nix b/pkgs/build-support/writers/test.nix index 656d127930fa..632815837c2d 100644 --- a/pkgs/build-support/writers/test.nix +++ b/pkgs/build-support/writers/test.nix @@ -18,6 +18,8 @@ let makeFSharpWriter writeBash writeBashBin + writeBabashka + writeBabashkaBin writeDash writeDashBin writeFish @@ -85,6 +87,10 @@ recurseIntoAttrs { end ''); + babashka = expectSuccessBin (writeBabashkaBin "test-writers-babashka-bin" '' + (println "success") + ''); + rust = expectSuccessBin (writeRustBin "test-writers-rust-bin" {} '' fn main(){ println!("success") @@ -189,6 +195,10 @@ recurseIntoAttrs { echo "success" ''); + babashka = expectSuccess (writeBabashka "test-writers-babashka" '' + (println "success") + ''); + haskell = expectSuccess (writeHaskell "test-writers-haskell" { libraries = [ haskellPackages.acme-default ]; } '' import Data.Default @@ -369,6 +379,36 @@ recurseIntoAttrs { '' ); + babashka-bin = expectSuccessBin ( + writeBabashkaBin "test-writers-wrapping-babashka-bin" + { + makeWrapperArgs = [ + "--set" + "ThaigerSprint" + "Thailand" + ]; + } + '' + (when (= (System/getenv "ThaigerSprint") "Thailand") + (println "success")) + '' + ); + + babashka = expectSuccess ( + writeBabashka "test-writers-wrapping-babashka" + { + makeWrapperArgs = [ + "--set" + "ThaigerSprint" + "Thailand" + ]; + } + '' + (when (= (System/getenv "ThaigerSprint") "Thailand") + (println "success")) + '' + ); + python = expectSuccess ( writePython3 "test-writers-wrapping-python" {