❄️

Netskopeが動いている環境でNixを使えるようにする(macOS向け)

に公開

TL;DR

  1. NIX_SSL_CERT_FILE 環境変数に、Netskopeの使っている証明書のファイルのパスを指定する。
  2. Nixをインストールするが、途中で失敗する。
  3. Keychainから出力したシステムにあるroot証明書全部と、Netskopeの証明書をバンドルしたファイルを作成する。
  4. 作成したファイルを、/etc/ssl/certs/ca-certificates.crt に置く。
  5. /etc/nix/nix.conf に、ssl-cert-file = /etc/ssl/certs/ca-certificates.crt を追記する。
  6. nix-daemonを再起動する。
  7. Nixをもう一回インストールする。

Netskopeを使っているとNixが使えない問題

NetskopeのTamperproof(改ざん防止)の、Protect Client configuration and resourcesという設定が有効になっている環境では、HTTPS通信を行う際に、Netskopeが通信の中身を確認するため、暗号化通信をTerminateする。そのため、一部ツールではアクセス先とTLS通信に利用される証明書が異なるとエラーになるケースがある。詳しくは「CLIツール向けNetskope証明書の設定」と「改ざん防止機能」の組み合わせに対する回避策 – CloudNative Inc. BLOGsを参照してほしい。

Nixでもこの問題は発生する。パッケージをリモートから取得する際に、以下のようなエラーが出る。

warning: error: unable to download 'https://cache.nixos.org/nar/0fmvraipkgqngblnb9c1mb0pv9bcazlk4cjjr86kqr5a5llivhna.nar.xz': SSL peer certificate or SSH remote key was not OK (60) SSL certificate problem: self-signed certificate in certificate chain; retrying in 290 ms

これは、Nixをインストールする際にも発生する。Netskopeを一時的にオフにするという方法もあるが、そもそも無効化できない環境では根本的な対策が必要となる。試行錯誤して、一応解決できたので、その方法について解説する。

Nixの初回インストール

Nixのインストールマニュアルに、Environment Variables - Nix Reference Manualという項目がある。ここに、NIX_SSL_CERT_FILEという環境変数の説明がある。この環境変数に、Netskopeの使っている証明書を発行しているCAの証明書を渡すことで、Nixがその証明書を信用して(?)、self-signed certificate in certificate chain というエラーが出なくなる。

$ export NIX_SSL_CERT_FILE=/etc/ssl/my-certificate-bundle.crt
$ curl -L https://nixos.org/nix/install | sh

これで問題は解決と思いきや、実はそうではない。

この設定でインストールを実行すると途中では停止せず、最後まで進んだように見えるが、途中で同じような問題が出ているようなログが出ている。

(省略)

~~> Setting up the default profile
installing 'nix-2.31.2'
building '/nix/store/pwwhjv2kxfpa11zra66my1fld1nqqah5-user-environment.drv'...
installing 'nss-cacert-3.113.1'
building '/nix/store/ifhkcxgdlj237bhhrqlaq1h57390h5ak-user-environment.drv'...
warning: error: unable to download 'https://nixos.org/channels/nixpkgs-unstable': SSL peer certificate or SSH remote key was not OK (60) SSL certificate problem: self-signed certificate in certificate chain; retrying in 279 ms
warning: error: unable to download 'https://nixos.org/channels/nixpkgs-unstable': SSL peer certificate or SSH remote key was not OK (60) SSL certificate problem: self-signed certificate in certificate chain; retrying in 519 ms
warning: error: unable to download 'https://nixos.org/channels/nixpkgs-unstable': SSL peer certificate or SSH remote key was not OK (60) SSL certificate problem: self-signed certificate in certificate chain; retrying in 1007 ms
warning: error: unable to download 'https://nixos.org/channels/nixpkgs-unstable': SSL peer certificate or SSH remote key was not OK (60) SSL certificate problem: self-signed certificate in certificate chain; retrying in 2691 ms
error: unable to download 'https://nixos.org/channels/nixpkgs-unstable': SSL peer certificate or SSH remote key was not OK (60) SSL certificate problem: self-signed certificate in certificate chain

~~> Setting up the nix-daemon LaunchDaemon
Alright! We're done!
Try it! Open a new terminal, and type:

  $ nix-shell -p nix-info --run "nix-info -m"

(省略)

この時点で、Nixそのもののインストールはできているのか、nix-daemonは稼働している状態になる。

問題の調査

この問題を解決するために調べてみると、/etc/nix/nix.conf または ./config/nix/nix.conf に、ssl-cert-file = '/path/to/ca-bundle.crt' を追記する、という解決策を見つけたのだが、これはなぜか効果がなかった。ドキュメント(nix.conf - Nix Reference Manual)には、NIX_SSL_CERT_FILESSL_CERT_FILE という環境変数で上書きできると書いてあるが、これについてはすでに設定済みなのに、やはり効果がない状態である。

ということで、さらに色々調べてみると、以下の投稿が見つかった。

これに倣ってやってみる。

証明書の設定

まず、Keychainに入っている証明書をエクスポートする。

security export -t certs -f pemseq -k /Library/Keychains/System.keychain -o /tmp/certs-system.pem
security export -t certs -f pemseq -k /System/Library/Keychains/SystemRootCertificates.keychain -o /tmp/certs-root.pem

エクスポートした証明書を1つのファイルにバンドルする。この時、Netskopeの証明書(/Library/Application Support/Netskope/STAgent/data/nscacert.pem)も一緒に入れる。

cat /tmp/certs-root.pem /tmp/certs-system.pem /Library/Application Support/Netskope/STAgent/data/nscacert.pem > /tmp/ca_cert.pem

作成したファイルを、/etc/nix に置く。

sudo mv /tmp/ca_cert.pem /etc/nix/

/etc/nix/nix.conf に、以下の設定を追記する。

ssl-cert-file = /etc/nix/ca_cert.pem

nix-daemonを再起動する。

sudo launchctl unload /Library/LaunchDaemons/org.nixos.nix-daemon.plist
sudo launchctl load /Library/LaunchDaemons/org.nixos.nix-daemon.plist

2回目のインストール

インストールする前に、前回のインストールプロセス中に出力されている、以下のファイルを戻しておく必要がある。元のファイルが *.backup-before-nix というプレフィックス付きの名前でバックアップされているので、それをオリジナルのファイルに戻しておく。

(省略)

 - back up /etc/bashrc to /etc/bashrc.backup-before-nix
 - update /etc/bashrc to include some Nix configuration
 - back up /etc/zshrc to /etc/zshrc.backup-before-nix
 - update /etc/zshrc to include some Nix configuration
 - back up /etc/bash.bashrc to /etc/bash.bashrc.backup-before-nix
 - update /etc/bash.bashrc to include some Nix configuration
 - back up /etc/zsh/zshrc to /etc/zsh/zshrc.backup-before-nix
 - update /etc/zsh/zshrc to include some Nix configuration

(省略)

これで、もう一回、Nixのインストールを実行する(環境変数の NIX_SSL_CERT_FILE は消しておく)。

$ curl -L https://nixos.org/nix/install | sh

うまくいけば、証明書に関するエラーは出なくなる。

動作確認

インストールが終わったら、新しくTerminalを開き直して、nixコマンドを実行してみる。

$ nix --version
nix (Nix) 2.31.2

インストールログの最後に出てくる、以下のコマンドも実行してみる。

$ nix-shell -p nix-info --run "nix-info -m"

すると、以下のような出力が出る。

warning: ignoring the client-specified setting 'build-users-group', because it is a restricted setting and you are not a trusted user
warning: ignoring the client-specified setting 'ssl-cert-file', because it is a restricted setting and you are not a trusted user
these 64 paths will be fetched (152.29 MiB download, 1143.28 MiB unpacked):
  /nix/store/3j563al2i894fvmf4g4h97xg3hnrhw02-DarwinTools-1

(中略)

copying path '/nix/store/xbpw0yw65al9vrnskqw9zycxlw0r2h1c-libresolv-83' from 'https://cache.nixos.org'...
error: unable to download 'https://cache.nixos.org/nar/0fmvraipkgqngblnb9c1mb0pv9bcazlk4cjjr86kqr5a5llivhna.nar.xz': Problem with the SSL CA cert (path? access rights?) (77) error setting certificate file: /etc/ssl/certs/ca-certificates.crt
error: some substitutes for the outputs of derivation '/nix/store/qhlflvwgl03w76gggvhiizs2qp56w8n3-bash-5.3p3.drv' failed (usually happens due to networking issues); try '--fallback' to build derivation from source
error: build of '/nix/store/hr71hx9fpkhx0ybwsqskbbcl8b5xb32a-bash-interactive-5.3p3.drv', '/nix/store/qhlflvwgl03w76gggvhiizs2qp56w8n3-bash-5.3p3.drv', '/nix/store/vdvhv8mhrwdplkzqi5y03ccdfglkbvjh-nix-info.drv', '/nix/store/wq5h3jz23fk2ga5y0bgiv256wggfgm7r-stdenv-darwin.drv' failed

まだエラーが出る!

ログによると、なぜか /etc/ssl/certs/ca-certificates.crt を参照している。確認してみると、リンクはあるが実態はない。

$ ls /etc/ssl/certs/ca-certificates.crt
/etc/ssl/certs/ca-certificates.crt@

$ ls -al /etc/ssl/certs/ca-certificates.crt
lrwxr-xr-x@ 1 root  wheel  41 Sep 23 00:24 /etc/ssl/certs/ca-certificates.crt@ -> /etc/static/ssl/certs/ca-certificates.crt

$ cat /etc/ssl/certs/ca-certificates.crt
cat: /etc/ssl/certs/ca-certificates.crt: No such file or directory

先ほど作成した /etc/nix/ca_cert.pem をコピーして配置する。

先ほどのコマンドをもう一度実行してみる。

$ nix-shell -p nix-info --run "nix-info -m"
warning: ignoring the client-specified setting 'build-users-group', because it is a restricted setting and you are not a trusted user
warning: ignoring the client-specified setting 'ssl-cert-file', because it is a restricted setting and you are not a trusted user
these 64 paths will be fetched (152.29 MiB download, 1143.28 MiB unpacked):
  /nix/store/3j563al2i894fvmf4g4h97xg3hnrhw02-DarwinTools-1

(中略)

copying path '/nix/store/pgrkqlci4gld1kp0qdhjzp1zazxblj2b-stdenv-darwin' from 'https://cache.nixos.org'...
 - system: `"aarch64-darwin"`
 - host os: `Darwin 25.0.0, macOS 26.0`
 - multi-user?: `yes`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.31.2`
 - channels(root): `"nixpkgs"`
 - nixpkgs: `/nix/store/kp6bn041bad548gi1b24sw4v8bsznyjc-nixpkgs/nixpkgs`

実行できた!

注意点・疑問点など

  • NIX_SSL_CERT_FILE 環境変数に指定すると、インストーラーはそこを参照するっぽいけど、nix-daemonは参照しないっぽい?
  • nix-daemonを再起動しても設定が反映されないことがあるっぽい?その場合はOSを再起動すると反映される。
  • NIX_SSL_CERT_FILE/etc/nix/nix.confssl-cert-fileで指定するファイルは、必要な証明書を全部含めないと、ちゃんと動かないっぽい?
  • その辺りの挙動は、インストーラーとnix-daemonで異なる?
  • /Library/LaunchDaemons/org.nixos.nix-daemon.plistに、<EnvironmentVariables>としてNIX_SSL_CERT_FILEを設定するという方法も見つけたけど、結果としてそれは不要っぽい。
  • 実行するコマンドによっては、~/.config/nix/nix.confの設定を、見たり見なかったりするような挙動があった。
  • 色々試しているので、もしかしたら変な状態になっていたことで、ここに書いたことは正しくないかもしれない。
GitHubで編集を提案

Discussion