Jump to content

Fortran: Difference between revisions

From NixOS Wiki
Initial page
 
Simplify setup, add example flake
Line 1: Line 1:
The Fortran front-end for gcc can be enabled by adding '''pkgs.gfortran.cc''' to '''environment.systemPackages'''. <ref>https://github.com/NixOS/nixpkgs/issues/61736</ref>
The Fortran front-end for gcc can be enabled by adding <code>pkgs.gfortran</code> to {{Nixos:option|environment.systemPackages}}.


The addition of '''.cc''' indicates the compiler itself without the gcc-wrapper Nix expressions used by '''gfortran'''. <ref>https://github.com/NixOS/nixpkgs/issues/20270</ref>
If both <code>gcc</code> and <code>gfortran</code> are included, the build may fail with an error of the form: "collision between gfortran-wrapper/bin/gcc and gcc-wrapper/bin/gcc".


If both '''gcc''' and '''gfortran''' (without '''.cc''') are included, the build may fail with an error of the form: "collision between gfortran-wrapper/bin/gcc and gcc-wrapper/bin/gcc".
An example [[Flakes|nix flake]]:
 
<syntaxhighlight lang="nix">
{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
 
  outputs = {nixpkgs, ...}: let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
  in {
    devShells.${system}.default = pkgs.mkShell {
      packages = with pkgs; [
        gfortran
      ];
    };
  };
}
</syntaxhighlight>
 
Run <code>nix develop</code> to enter the nix shell, then compile with <code>gfortran myfile.f</code>.

Revision as of 20:17, 31 May 2025

The Fortran front-end for gcc can be enabled by adding pkgs.gfortran to environment.systemPackages.

If both gcc and gfortran are included, the build may fail with an error of the form: "collision between gfortran-wrapper/bin/gcc and gcc-wrapper/bin/gcc".

An example nix flake:

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

  outputs = {nixpkgs, ...}: let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
  in {
    devShells.${system}.default = pkgs.mkShell {
      packages = with pkgs; [
        gfortran
      ];
    };
  };
}

Run nix develop to enter the nix shell, then compile with gfortran myfile.f.