首页 > 解决方案 > 如何在 NixOS 上构建 Swift 项目?

问题描述

我正在尝试在 NixOS 上构建一个简单的 Swift 项目。以下是重现的步骤:

git clone https://github.com/PerfectlySoft/PerfectTemplate.git
cd PerfectTemplate
touch shell.nix

添加shell.nix以下内容:

with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "swift-env";

  buildInputs = [
    openssl
    stdenv
    binutils
    cmake
    pkgconfig
    curl
    glibc
    icu
    libblocksruntime
    libbsd
    libedit
    libuuid
    libxml2
    ncurses
    sqlite
    swig
  ];

}

nix-shell --pure shell.nix从那里跑步swift build,给我:

gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-target’
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fmodule-name=PerfectCZlib’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fmodules-cache-path=/home/dmi3y/Dev/Swift/PerfectTemplate/.build/x86_64-unknown-linux/debug/ModuleCache’
gcc: error: unrecognized command line option ‘-fblocks’

我很感激这方面的任何帮助,因为我对 nix-shell 和 NixOS 非常陌生。

编辑

我把我改成shell.nix这样:

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "swift-env";

  buildInputs = with pkgs; [
    openssl
    clang
    swift
  ];

  shellHook = ''
    CC=clang
  '';
}

但是,当我运行时,我现在得到nix-shellswift build

[1/25] Compiling PerfectCZlib zutil.c
[2/25] Compiling PerfectCZlib uncompr.c
[3/25] Compiling PerfectCZlib inftrees.c
[4/25] Compiling PerfectCZlib inflate.c
[5/25] Compiling PerfectCZlib trees.c
[6/25] Compiling PerfectCZlib inffast.c
[7/25] Compiling PerfectCZlib infback.c
[8/25] Compiling PerfectCZlib gzwrite.c
/nix/store/iz6k7x3l14ykr56crz3dizas1rrkcyzr-clang-wrapper-7.1.0/resource-root/include/module.modulemap:24:8: error: redefinition of module '_Builtin_intrinsics'

#  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
   ^
1 warning and 3 errors generated.
1 warning and 3 errors generated.

完整的日志可以在这里找到。

这现在看起来像是缺少库依赖项或版本冲突。

作为记录,以下是有关我的设置的其他信息,可能会提供更多信息:

$ nix-channel --list                                                    
home-manager https://github.com/rycee/home-manager/archive/master.tar.gz
nixos https://nixos.org/channels/nixos-19.09  

$ nix-shell --run 'swift -version'                                      
Swift version 5.0.2 (swift-5.0.2-RELEASE)
Target: x86_64-unknown-linux-gnub

在网上搜索后,我发现了 Swift 编译器的 Jira 问题和这个特定的评论。我决定clang从依赖项中排除并且只包含swift,因为这种组合似乎是冲突的:

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "swift-env";

  buildInputs = with pkgs; [
    openssl
    swift
  ];

  shellHook = ''
    CC=clang
  '';
}

那行得通!我终于能够构建这个项目了!

标签: swiftgccnixos

解决方案


由于您使用了该--pure选项,因此您以前的环境中的任何内容都不会导入到 shell 中。这包括swift二进制文件本身。所以首先我会添加swiftbuildInputs.

您得到的错误表gcc明甚至不理解正在传递的命令行选项。我对 Swift 一无所知,但在看到之后我认为它需要一个不同的编译器。果然,经过快速搜索,它看起来像是基于 llvm 的。因此,您还需要添加clang到您的构建输入中。此外,您需要使用mkShell而不是mkDerivation设置正确的环境。

前者是后者的包装器,专门设计用于设置 nix-shell,并确保正确处理环境。您需要传入该shellHook属性以覆盖默认的 c 编译器以clang使用CCenvvar。这shellHook是在 shell 开始时运行任意命令的一种方式。

最后一点,该nix-shell命令默认搜索,shell.nix因此您不必将其作为参数传递。如果shell.nix不存在,那么它会尝试default.nix. 如果两者都不存在,那么您必须明确指定 nix 表达式位置。

TLDR

把它们放在一起:

# a more idiomatic way to import nixpkgs, overridable from the cmdline
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "swift-env";

  buildInputs = with pkgs; [
    # your other inputs
    clang
    swift
  ];

  shellHook = ''
    CC=clang
  '';
}

为了确保我的逻辑是正确的,我下载了 repo 并放弃了它。一切都编译并成功运行。


推荐阅读