Skip to content

Latest commit

 

History

History
87 lines (61 loc) · 2.63 KB

nix.md

File metadata and controls

87 lines (61 loc) · 2.63 KB

Using Babashka with Nix

Babashka is packaged in nixpkgs and can be easily used from the Nix package manager.

The following assumes a recent installation of nix and uses the unstable nix cli and Flakes.

To enable the unstable cli and flakes add the following to /etc/nix/nix.conf:

extra-experimental-features flakes nix-command

Imperative install on Nix

To imperatively install nix for the current user, run nix profile install babashka.

Declarative global install on NixOS

To install babashka for all users on a NixOS system, place it in environment.systemPackages in your configuration.nix:

{ pkgs, ... }:
{
    environment.systemPackages = with pkgs; [
        babashka
    ];
}

Then run nixos-rebuild switch, to activate the new configuration.

Declarative per-user install with home-manager

You can install babashka for a specific user using home-manager. Add the following to your ~/.config/nixpkgs/home.nix:

{ pkgs, ... }:
{
    home.packages = with pkgs; [
        babashka
    ];
}

Then run home-manager switch, to activate the new configuration.

Per project install with direnv

To make babashka available on a per-project basis, you can use direnv.

Create a file .envrc in the project directory with the following contents:

use flake

Create a file flake.nix in the project directory with the following contents:

{
  outputs = {nixpkgs, ...}: let
    supportedSystems = ["x86_64-linux" "x86_64-darwin"];
    forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
    nixpkgsFor = system: import nixpkgs {inherit system;};
  in {
    devShell = forAllSystems (system: let
      pkgs = nixpkgsFor system;
    in
      pkgs.mkShell {
        packages = with pkgs; [
          babashka
        ];
      });
  };
}

After running direnv allow, babashka should be available on the $PATH, when you are inside the project directory.

Write Babashka Application

You can write babashka scripts with native dependencies using WriteBabashkaApplication.

The WriteBabashkaApplication repository has an example flake.nix using cowsay as an external dependency.

You can download that example, and then build the application using nix build or run it using nix run.