Is it possible to call a writeScriptBin from another nix file?

This nix file nixifies a script after which I can call it in the cli. How can I use/call this script, “my-custom-script”, inside another nix file.

{ config
, lib
, pkgs
, ...
}:
with lib; let
  bashScript = name: scriptDependencies: (
    pkgs.resholve.writeScriptBin
      "${name}"
      {
        interpreter = "${pkgs.bash}/bin/bash";
        inputs = scriptDependencies;
        fake.external = [ "sudo" "ping" "mount" "umount" ];
        execer = [
          "cannot:${pkgs.git}/bin/git"
          "cannot:${pkgs.gzip}/bin/uncompress"
          "cannot:${pkgs.networkmanager}/bin/nmcli"
          "cannot:${pkgs.nixos-install-tools}/bin/nixos-generate-config"
          "cannot:${pkgs.nixos-install-tools}/bin/nixos-install"
          "cannot:${pkgs.nixos-rebuild}/bin/nixos-rebuild"
          "cannot:${pkgs.nix}/bin/nix"
          "cannot:${pkgs.nix}/bin/nix-collect-garbage"
          "cannot:${pkgs.openssh}/bin/ssh"
          "cannot:${pkgs.p7zip}/bin/7z"
          "cannot:${pkgs.p7zip}/bin/7za"
          "cannot:${pkgs.procps}/bin/pkill"
          "cannot:${pkgs.rsync}/bin/rsync"
          "cannot:${pkgs.sway}/bin/swaymsg"
          "cannot:${pkgs.systemd}/bin/systemctl"
          "cannot:${pkgs.util-linux}/bin/swapon"
          "cannot:${pkgs.wl-clipboard}/bin/wl-copy"
          "cannot:${pkgs.wpa_supplicant}/bin/wpa_passphrase"
        ];
      }
      (builtins.readFile ../../scripts/${
      name}.sh)
  );

  my-custom-script = bashScript "my-custom-script" (with pkgs; [
    coreutils
    util-linux
    gawk
    gnugrep
    gnused
    parted
    cryptsetup
    exfat
    e2fsprogs # ext4
  ]);
in
{
  config = {
    environment.systemPackages = with pkgs; [
      my-custom-script
    ];
  }
}

I’d like to call my-custom-script inside a udev run script.

{ config
, lib
, pkgs
, ...
}:
with lib;
{
  config = lib.mkIf cfg.enable {
    # udevadm info -a -p /sys/block/sdc | ag serial
    services.udev.extraRules =
      let
        usbAddScript = pkgs.writeScript "usb-add-script" ''
          #!${pkgs.stdenv.shell}
          my-custom-script
        '';
      in
      ''
        ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0000", ATTRS{idProduct}=="0000", ATTRS{serial}=="xxxxxxxxxxxx", RUN+="${usbAddScript}"
      '';
  };
}

You need to engineer a way to get the value of my-custom-script into scope in the file in question. Once you do, you can use ${my-custom-script}/bin/my-custom-script elsewhere to refer to it.

Generally, I wouldn’t write a nixos module just for a script like this. A more useful way to put it in its own file would be to make it callPackageable, then you can instantiate it in more varied ways, including just instantiating it multiple times. That wouldn’t hurt anything due to nix deduping everything, as long as it’s instantiated identically.

You can also consider:

  • making a read-only option just to expose the value to other modules
  • using _module.args to get it into the argument list supplied to all the modules
1 Like

I was thinking about making an option, I didn’t know about the read-only part. Everything else I’ll look into. Thank you.

The nixos module for the script contains -+10 bash scripts and -+10 python scripts.