swift - 如何在 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-shell
:swift 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
'';
}
那行得通!我终于能够构建这个项目了!
解决方案
由于您使用了该--pure
选项,因此您以前的环境中的任何内容都不会导入到 shell 中。这包括swift
二进制文件本身。所以首先我会添加swift
到buildInputs
.
您得到的错误表gcc
明甚至不理解正在传递的命令行选项。我对 Swift 一无所知,但在看到之后我认为它需要一个不同的编译器。果然,经过快速搜索,它看起来像是基于 llvm 的。因此,您还需要添加clang
到您的构建输入中。此外,您需要使用mkShell
而不是mkDerivation
设置正确的环境。
前者是后者的包装器,专门设计用于设置 nix-shell,并确保正确处理环境。您需要传入该shellHook
属性以覆盖默认的 c 编译器以clang
使用CC
envvar。这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 并放弃了它。一切都编译并成功运行。
推荐阅读
- c# - 在 C# 中按 dateTime 对统一文件夹中的模型重新排序
- angular - 我有大约 30 个社交链接字段来获取每个我想使用 formArray 构建的 url
- c++ - 需要使用递归c ++计算算术级数的总和
- angular - 未定义标识符“已提交”
- javascript - 如何在javascript中将封闭的双引号转换为单引号?
- visual-studio - API Gateway 的 SAM 模板在 Visual Studio 中存在错误(作为无服务器应用程序解决方案的一部分)
- flask - 使用 pytest 测试烧瓶应用程序时,如何解决身份验证标头/cookie 丢失问题?
- css - CSS - 带阴影的 ZigZag 右边框
- aop - AspectJ 中代码内和代码内指示符之间的区别
- api - 使用 JSON Web Token (JWT) 进行身份验证和授权