Skip to content

Packaging ADDA for nix ✅ #340

Open
Open
@arunoruto

Description

@arunoruto

I have been an avid Nix user for quite some time, and I will be using ADDA in the future; hence, I took some time to package it up for Nix! This isn't much of an issue, but more for making my small side project public and see if someone is a nix package of ADDA.

For those who are not aware what nix is: it can be three things:

  1. A functional programming language,
  2. THE biggest package manager for Linux/Unix based systems, even topping Arch/AUR! source,
  3. A declarative operating system (NixOS) and environment manager.

The package manager itself can be installed on any system!

The key advantage of nix is its reproducibility and huge amount of packages. It can be a bit finicky, but all in all, it is a fantastic tool!

All the dependencies of ADDA are already provided in nixpkgs, and by just switching an input argument, it will compile the normal sequential version, the MPI version, or the OpenCL version with clBLAS included!

Here is the current version of the `package.nix` file:
{
  stdenv,
  lib,
  fetchFromGitHub,
  which,
  gfortran,
  fftw,
  mpi,
  opencl-headers,
  ocl-icd,
  clfft,
  clblas,

  target ? "seq",
  # target ? "mpi",
  # target ? "ocl",
}:
let
  binaryName =
    if target == "seq" then
      "adda"
    else if target == "mpi" then
      "adda_mpi"
    else if target == "ocl" then
      "adda_ocl"
    else
      throw "Unknown target: ${target}";
  options = builtins.concatStringsSep " " (
    [ "NO_GITHASH" ] ++ (lib.optionals (target == "ocl") [ "OCL_BLAS" ])
  );
  flags = builtins.concatStringsSep " " (
    # create a list of name-value pairs for make flags
    lib.attrsets.mapAttrsToList (name: value: "${name}=${value}") {
      COMPILER = "gnu";
      OPTIONS = ''"${options}"'';
      FORT_LIB_PATH = "${gfortran.cc}/lib";
    }
    ++ [ target ] # append target to the end of the generated list, no name-value pair
  );
in
stdenv.mkDerivation (finalAttrs: {
  pname = binaryName;
  # version = "1.4.0";
  version = "1.4.0-unstable-2025-05-26";

  src = fetchFromGitHub {
    owner = "adda-team";
    repo = finalAttrs.pname;
    rev = "ac6c6be366a6f2ec6a096d8adce2ed7fffc417c2";
    hash = "sha256-sXHgAT5khKo5P9jRl7j+LDtfoXuDyttnZK6rbMWt1w4=";
    # tag = "v${finalAttrs.version}";
    # hash = "sha256-mGCWrmdut3sgWATo/Y0HCVlA67425lH8n2A1RdL7Kzg=";
  };

  nativeBuildInputs =
    [
      which
      gfortran
      fftw
    ]
    ++ (lib.optionals (target == "mpi") [
      mpi
    ])
    ++ (lib.optionals (target == "ocl") [
      opencl-headers
      ocl-icd
      clfft
      clblas
    ]);

  preBuild = ''
    makeFlagsArray+=(${flags})
    cd src/
  '';

  installPhase = ''
    runHook preInstall

    install "${target}/${binaryName}" -Dm755 -t "$out"/bin

    runHook postInstall
  '';

  meta = with lib; {
    homepage = "https://github.com/adda-team/adda";
    description = "light scattering simulator based on the discrete dipole approximation";
    license = licenses.gpl3;
    platforms = [ "x86_64-linux" ];
    maintainers = with maintainers; [ arunoruto ];
    mainProgram = binaryName;
  };
})
The latest version can always be found in my flake repository: https://github.com/arunoruto/flake/blob/main/pkgs/adda/package.nix

If someone is eager to try it out, just install nix (multi user) and you can run one of the three commands:

nix run --accept-flake-config github:arunoruto/flake#adda -- <options>
nix run --accept-flake-config github:arunoruto/flake#adda-mpi -- <options>
nix run --accept-flake-config github:arunoruto/flake#adda-ocl -- <options>

and replace <options> with the CLI params.

This setup can be also used to test builds, since nix provides to integrate unit tests and only let a build pass if the tests are passing. Since the dependencies are already in nixpkgs and will be fetched upon building, the first point provided in #316 (comment) would be solved!

If there is an interest, I could also make a PR to the nixpkgs repository and make ADDA available there!

P.S. I wanted to ask if there will be a new release in the future? If I use the v.1.4.0 tag I get some weird warnings and it errors out...

Metadata

Metadata

Assignees

Labels

comp-ScriptsRelated to Makefiles, wrappers, developer and testing scriptsusabilityMakes using code more convenient

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions