æè¿ãC ++ã«ã¯pipãnpmãmavenãcargoãªã©ã®ç¬èªã®ããã±ãŒãžãããŒãžã£ãŒãå¿ èŠã§ãããšããå€ãã®è©±ããããŸãã ãã¹ãŠã®ç«¶åä»ç€Ÿã«ã¯ãéæšæºã©ã€ãã©ãªãæ¥ç¶ããããã®ã·ã³ãã«ã§æšæºåãããã¡ã«ããºã ããããŸãã C ++ã§ã¯ã誰ããã§ããéãè¡åããŸãïŒèª°ããUbuntuãCentOSããã®ä»ã®ãã£ã¹ããªãã¥ãŒã·ã§ã³çšã®ããã±ãŒãžã®ãªã¹ããREADMEã«æžã蟌ã¿ã誰ããgitãµãã¢ãžã¥ãŒã«ãšã¹ã¯ãªããã䜿çšããŠããããæ§ç¯ãã誰ããCMake ExternalProjectã䜿çšãã誰ãããã¹ãŠã®ãœãŒã¹ãã³ããŒããŸã1ã€ã®å·šå€§ãªãªããžããªã誰ããDockerãŸãã¯Vagrantã®ã€ã¡ãŒãžãäœæããŸãã
ãã®åé¡ã解決ããããã«ãã¹ã¿ãŒãã¢ãã-biicodeãäœæãããŸããããç Žç£ããå°æ¥ã¯äžæã§ãã 代ããã«ã³ãã³ãç»å Žãã競åä»ç€Ÿã®åç©åã®åç©åã cget ã ãã³ã¿ãŒ ã cpm ã qpm ã cppget ã pacm ãããã«ã¯gradle for c ++ã è£å®ããŸããã
äžèšã®ãããã®æ¹æ³ã«ãæºè¶³ã§ããŸããã§ããã Conanã®ããã±ãŒãžãæžãå§ããŸããããå€æ°ã®ããã¯ãæªéçºã®APIãã¬ã€ãã©ã€ã³ã®æ¬ åŠããããŠãã®çµæãä»ã®äººã®ããã±ãŒãžãåå©çšããå¯èœæ§ãäœãããšã«ééããŸããã ãããŠãç§ã¯NixOSã®ããã±ãŒãžãããŒãžã£ãŒã®ã¢ã€ãã¢ãæ¬åœã«å¥œãã ã£ãããšãæãåºããŸãã ã ãããŠãç§ã¯èããŸãã-éåžžã®ããã±ãŒãžãããŒãžã£ãŒãåãåé¡ã解決ããã®ã§ããã°ããªãC ++å°çšã®ããã±ãŒãžãããŒãžã£ãŒãäœæããã®ã§ããïŒ ããã±ãŒãžã®èª¬æã«é¢ããŠã¯ãååã«æè»ã§ã·ã³ãã«ã§ããããšãå¿ èŠã§ãã ãããŠãNixã¯ãã®åœ¹å²ã«ãŽã£ããã§ããã
ããã§ãNixãç§ãã¡ã«äžãããã®ïŒ
- 1ã€ã®ã³ãã³ã
nix-shell
ãããžã§ã¯ãç°å¢ãæ§ç¯ããæºåãæŽããæ©èœã -
nixpkgs
æ¢è£œããã³ãµããŒãããã±ãŒãžã - ãªããžããªãŒã®ããã±ãŒãžãã掟çããã±ãŒãžãäœæããæ©èœïŒã³ãŒããã³ããŒããã«ïŒã
- äŸåé¢ä¿ã®C / C ++ã©ã€ãã©ãªã ãã§ãªããå¿ èŠãªããŒã«ïŒCMakeãGCCïŒãä»ã®ãšã³ã·ã¹ãã ã®ãããžã§ã¯ãïŒnpmãpipïŒããµãŒãã¹ïŒredisïŒãæå®ããæ©èœã
- ã³ãããããç°å¢ããã€ã³ãããæ©èœã ããã¯ãããšãã°ãmasterãã©ã³ããboost 1.55ãšdevel-1.60ã䜿çšã§ããããšãæå³ããŸãã ãã©ã³ããããã©ã³ãã«åãæ¿ãããšãNixã¯ç®çã®ããŒãžã§ã³ã®ç°å¢ãèªåçã«æ§æããŸããããã«ã¯1ç§ãããããŸããïŒã¢ã»ã³ããªãæ¢ã«ãã£ãã·ã¥ã«ããå ŽåïŒã
- éäŸµå ¥æ§-ãããžã§ã¯ãã¯Nixã«äŸåããããã®äœ¿çšã¯èª°ã«ãšã£ãŠãå人çãªåé¡ã§ãã cmakeã®ãã¹ãŠã®æ£ãããªãã·ã§ã³ãæå®ããŠããã¹ãŠã®äŸåé¢ä¿ãæåã§ïŒãŸãã¯ãæ°ã«å ¥ãã®ããã±ãŒãžãããŒãžã£ãŒã«ãã£ãŠïŒåéã§ããŸãã
ããã¯ã¹ãšã¯
Nixã¯ãããã±ãŒãžãããŒãžã£ãŒã®ããŒãºã«åãããŠèª¿æŽãããé¢æ°åããã°ã©ãã³ã°èšèªã§ãïŒHaskellã³ãã¥ããã£ã§äººæ°ãåããã®ã¯é©ãããšã§ã¯ãããŸããïŒã ããã±ãŒãžã¢ã»ã³ããªã¯ãNixã§ã®é¢æ°èšç®ã§ãã é¢æ°åããã°ã©ãã³ã°èšèªã®å Žåãšåæ§ã«ãåãåŒæ°ã䜿çšããŠé¢æ°ãç¹°ãè¿ãåŒã³åºããšãåãçµæãçæãããŸãïŒãã€ããªããã±ãŒãžïŒã ããã¯ãããã±ãŒãžããã£ãã·ã¥ã§ããããšãæå³ããŸããããã¯ãNixãè¡ãããšã§ãããã¹ãŠã®ã¢ã»ã³ããªã¯ã /nix/store/$HASH-$PKGNAME
æ ŒçŽãããŸãã ããã«ããããã¯ãŒã¯äžã®ä»ã®èª°ããåãããã·ã¥ãæã€ããã±ãŒãžãæã£ãŠãããã©ããã確èªã§ããŸããæã£ãŠããå Žåã¯ããã®äººãããã€ããªããã±ãŒãžãããŠã³ããŒãããŸãã
ãããã£ãŠãNixã®ãããã±ãŒãžãïŒããã§ã¯æŽŸçïŒã¯é¢æ°ã§ããããäŸåé¢ä¿ãã¯ãã®é¢æ°ã®åŒæ°ã§ãã ãªããžããªïŒ NixPkgs
ïŒãšã¯äœã§ããïŒ ãããåŒæ°ãæããªãé¢æ°ã§ãããå€ãã®ããã±ãŒãžãè¿ããŸãã ãªããžããªã䜿çšããã«ã¯ããã¹ãŠã®7344ããã±ãŒãžãåéããå¿
èŠããããŸããïŒ ããïŒ Nixã¯é
延èšèªã§ããã€ãŸããæããã«å¿
èŠã«ãªããŸã§äœãèšç®ãããŸããã ãŸãããŠãŒãã£ãªãã£ã䜿çšããŠããã±ãŒãžããå¿
èŠãã«ããããšãã§ããŸãã
æå°éã®ç°å¢
ãããã£ãŠãNixã䜿çšããåã«ãã€ã³ã¹ããŒã«ããå¿
èŠããããŸãã ãããè¡ãã«ã¯ãLinuxãã£ã¹ããªãã¥ãŒã·ã§ã³å
šäœïŒNixOSïŒã䜿çšãããããæ°ã«å
¥ãã®OSã«åå¥ã«ããã±ãŒãžãããŒãžã£ãŒãã€ã³ã¹ããŒã«ããŸã ïŒLinuxããã³MacOSããµããŒããããŠããŸãïŒã ãã¹ãŠã®Nixã®å
¬éã¯ã /nix
ãã£ã¬ã¯ããªãšããŒã ãã£ã¬ã¯ããªå
ã®ãã¡ã€ã«ïŒ ~/.nix-channel
ã .nix-defexpr
ã .nix-profile
ïŒã«.nix-profile
ã
~/.nix-profile
ã¯ããŠãŒã¶ãŒãèŠæ±ããããã±ãŒãžãžã®ã·ã³ããªãã¯ãªã³ã¯ãä¿åããŸãã ãŠãŒã¶ãŒçšã§ã¯ãªãããããžã§ã¯ãçšã«ç°å¢ãæ§æããå¿
èŠããããŸãã ãããè¡ãã«ã¯ã nix-shell
ãŠãŒãã£ãªãã£ã䜿çšããŸããå
¥åã§æå®ãããNixåŒãå®è¡ããbashã·ã§ã«ãèµ·åããŸããbashã·ã§ã«ã§ã¯ãçµæïŒããã³ããã®ã¿ïŒã䜿çšã§ããŸãã ç§ãã¡ã¯ãã§ãã¯ããŸãïŒ
bash-3.2$ nix-shell -p stdenv [nix-shell:~]$
ããã§ã¯ãïŒ- -p
ïŒ stdenv
ããã±ãŒãžãåŒãšããŠäœ¿çšããŸãã stdenv
ã¯ãã³ã³ãã€ã©ãmakeãããã³ãã®ä»ã®éèŠãªèŠçŽ ãå«ãæå°éã®ç°å¢ã§ãã
ããã±ãŒãžæ§ç¯ç°å¢
åŒæ°ãªãã§nix-shell
ãå®è¡ãããšãåŒã¯default.nix
ãã¡ã€ã«ããèªã¿åãããŸãã äœæããïŒ
{ pkgs ? import <nixpkgs> {} }: let stdenv = pkgs.stdenv; in rec { myProject = stdenv.mkDerivation { name = "my-project"; }; }
ããã§ã¯ãå
¥åãšããŠãªããžããªãŒãåãå
¥ãïŒãã©ã¡ãŒã¿ãŒãæå®ãããŠããªãå Žåã¯ãæšæºã®nixpkgs
ã€ã³ããŒãããïŒããããžã§ã¯ãç°å¢ã®ãããã±ãŒãžããè¿ãnixpkgs
ã NixOSãªããžããªããæ°ããCMakeãBoostãGoogle Testãè¿œå ããŸãã
# ... myProject = stdenv.mkDerivation { name = "my-project"; nativeBuildInputs = [ pkgs.cmake ]; buildInputs = [ pkgs.boost pkgs.gtest ]; };
ããã§ãbuildInputsã¯ãã«ãã«å¿
èŠãªäŸåé¢ä¿ã§ãã ä»ã«nativeBuildInputsãããã®ã¯ãªãã§ããïŒ åé¡ã¯ãNixãã¯ãã¹ã³ã³ãã€ã«ããµããŒãããŠããããšã§ãã ããã§ãbuildInputsããã±ãŒãžã¯ã¿ãŒã²ããããŒã«ãã§ãŒã³ã«ãã£ãŠãã«ããããnativeBuildInputsã¯éåžžã®ãã¹ãããŒã«ãã§ãŒã³ã䜿çšããŠãã«ããããå¿
èŠããããšèšããŸãã ãŸãã propagatedBuildInputs
ããããŸããããã±ãŒãžã®ãã¹ãŠã®ãŠãŒã¶ãŒã«äŸåé¢ä¿ãè¿œå ãããŸãã
ããã§ã次ånix-shell
ãåŒã³åºããšãNixã¯å¿
èŠãªãã€ããªããã±ãŒãžãããŠã³ããŒãããç°å¢å€æ°ãèšå®ããŠãCMakeãªã©ã®æšæºããŒã«ã䜿çšããŠã©ã€ãã©ãªãèŠã€ããããã«ããŸãã
find_package(Boost 1.60 REQUIRED COMPONENTS system thread) find_path(GTEST_INCLUDE_DIRS NAMES gtest/gtest.h PATH_SUFFIXES gtest)
éçºè
ã¯cmake . && make
ã®ã¿ãå®è¡cmake . && make
cmake . && make
ã nix-shell
ã«å
¥ããšãã«åœŒã«éç¥ãnix-shell
ïŒ
myProject = stdenv.mkDerivation { # ... shellHook = ['' echo Welcome to myproject! echo Run \'mkdir build && cd build && cmake .. && make -j\' to build it. '']; };
nixpkgs
ãªãäŸåé¢ä¿ãæ§ç¯ãnixpkgs
次ã«ããããžã§ã¯ãã«cppformatãè¿œå ããŸãã ãŸãã nixpkgs
ã§æ¢ããŸãïŒ
$ nix-env -qaP | grep cppformat $ nix-env -qaP | grep cpp-format
空ã§ãã ç¬èªã®åŒãäœæããå¿ èŠããããŸãã 幞ããªããšã«ãããã¯ããã10è¡ã§ãã ãããããletãã«è¿œå ããŸãã
# ... let stdenv = pkgs.stdenv; fetchurl = pkgs.fetchurl; cppformat = stdenv.mkDerivation rec { version = "2.1.0"; name = "cppformat-${version}"; src = fetchurl { url = "https://github.com/cppformat/cppformat/archive/${version}.tar.gz"; sha256 = "0h8rydgwbm5gwwblx7jzpb43a9ap0dk2d9dbrswnbfmw50v5s7an"; }; buildInputs = [ pkgs.cmake ]; enableParallelBuilding = true; }; in rec { # ... buildInputs = [ # ... cppformat ]; # ...
次ã«ã nix-shell
次ã«å®è¡ãããšãã«ãNixã¯cppformatãœãŒã¹ãããŠã³ããŒãããcmakeã䜿çšããŠã³ã³ãã€ã«ããŸãïŒãããžã§ã¯ãã§ã¯cmakeã䜿çšããŠãããããæšæºã®ã ./configure && make install
ãã®ä»£ããã«ã cmake . && make install
/nix/store
ãã«ãããŸãã ä»ã®ã»ãšãã©ã®ããã±ãŒãžãããŒãžã£ãŒã®ãŠãŒãã£ãªãã£ãšã¯ç°ãªãã次ã®ç¹ã«æ³šæããŠãã ããã
- ã¢ã»ã³ããªã倱æããå ŽåããœãŒã¹ã¯åã³ãã³ãã¢ãŠããããŸããã
- åŒãå€æŽãããšãããã±ãŒãžãåã³ã³ãã€ã«ãããŸãã åŸã§åŒãããŒã«ããã¯ããããšã«ããå Žåããã¡ã€ã«ã®å€æŽæ¥ãå€æŽãããŠãïŒãã©ã³ã/ã³ããããå€æŽããå Žåã«äŸ¿å©ïŒããã£ãã·ã¥ã®å€ãããã±ãŒãžãèªåçã«äœ¿çšãããŸãã
ãªããžããªããããã±ãŒãžãå€æŽããŸã
ãªããžããªã«ããã±ãŒãžãååšããããšããããŸãããåžæã©ããã«ãã«ããããŠããŸããã ç¹å®ã®ããŒãžã§ã³ãåéããããããé©çšããç¹å®ã®ãã©ã°ã䜿çšããå¿ èŠããããŸãã Nixã䜿çšãããšããªããžããªããã³ãŒããã³ããŒã¢ã³ãããŒã¹ãããããšãªããããè¡ãããšãã§ããŸãã
cpp-netlib = pkgs.cpp-netlib.overrideDerivation(oldAttrs: { postPatch = '' substituteInPlace CMakeLists.txt \ --replace "CPPNETLIB_VERSION_PATCH 1" "CPPNETLIB_VERSION_PATCH 3" ''; cmakeFlags = oldAttrs.cmakeFlags ++ [ "-DCMAKE_CXX_STANDARD=11" ]; src = fetchFromGitHub { owner = "cpp-netlib"; repo = "cpp-netlib"; rev = "9bcbde758952813bf87c2ff6cc16679509a40e06"; # 0.11-devel sha256 = "0abcb2x0wc992s5j99bjc01al49ax4jw7m9d0522nkd11nzmiacy"; }; });
ãªããžããªå ã®ããã±ãŒãžãå€æŽããŸã
ãªããžããªã®å
ã®Xã«åºã¥ããŠæŽŸçããã±ãŒãžX 'ãæ§ç¯ããèªå®
ã§äœ¿çšã§ããŸãã ããã«ããªããžããªå
ã®äžéšã®ããã±ãŒãžYãXã«äŸåããŠããå ŽåãåŒãç¶ãå€ãããŒãžã§ã³ã䜿çšãããŸãã ãããããªããžããªå
ã®ããã±ãŒãžãå€æŽããå¿
èŠãããå Žåãã€ãŸã ä»ã®100,500åã®ããã±ãŒãžã§äœ¿çšãããããã«ïŒ ãã®å ŽåãNixã«ã¯ããŒã«ããããŸãã æšæºã®GCC 4.9ã®ä»£ããã«nixpkgs
ã䜿çšããŠãnixpkgsããããŒã¹ããåæ§ç¯ããŸãã
{ nixpkgs ? import <nixpkgs> {} }: let overrideCC = nixpkgs.overrideCC; stdenv = if ! nixpkgs.stdenv.isLinux then nixpkgs.stdenv else overrideCC nixpkgs.stdenv nixpkgs.gcc5; pkgs = nixpkgs.overridePackages (self: super: { boost = super.boost.override { stdenv = stdenv; }; });
ããã§ãåŒæ°åãnixpkgs
ããnixpkgs
ã«å€æŽããå¿
èŠã«nixpkgs
ãŠããŒã¹ããæ§ç¯ããã掟ççãªnixpkgs
ãªããžããªãäœæããŸãã ããã§ãããŒã¹ãã«äŸåããä»ã®ãã¹ãŠã®ããã±ãŒãžãåæ§ç¯ããŠãã¢ã»ã³ããªã䜿çšããå¿
èŠããããŸãã ãã¡ãããåŒã®äžã§äœ¿çšãããããã±ãŒãžã®ã¿ãïŒååž°çã«ïŒåæ§ç¯ãããŸããçµå±ãNixã¯æ ãè
ã§ãã
ãµãŒãããŒãã£ã®ããã±ãŒãžãããŒãžã£ãŒããã³ãã©ãããã©ãŒã ãšã®çµ±å
ããã§ããã¹ãŠãç°¡åã§ã-Nixã¯.NETãEmacsãGoãHaskellãLuaãNodeãPerlãPHPãPythonãRustã®ããã±ãŒãžã®ãã«ãããµããŒãããŠããŸãã ãããã®äžéšã«ã€ããŠã¯ãNixããã€ãã£ãããã±ãŒãžãããŒãžã£ãŒããçŽæ¥ããã±ãŒãžã䜿çšã§ãããšããçµ±åããããŸãã
nativeBuildInputs = [ pkgs.cmake pkgs.pkgconfig nodePackages.uglify-js ];
NixãYouCompleteMeã«çµ±åãã
YouCompleteMeã¯ãããããIDEã®äžéšã§ã¯ãªãæã人æ°ã®ããC ++ã³ãŒãè£å®ãšã³ãžã³ã§ãã 圌ã¯VimãèŸããŸãããããã§ã«Atomãä»ã®ãšãã£ã¿ãŒåãã®ç§»æ€çããããŸãã 以åã®éçºè ãã·ã¹ãã çšã«åå¥ã«æ§æããå¿ èŠããã£ãå Žåãä»ã§ã¯æ®éçã«è¡ãããšãã§ããŸãã
def ExportFromNix(): from subprocess import Popen, PIPE import shlex cmd = "nix-shell -Q --pure --readonly-mode --run 'echo $NIX_CFLAGS_COMPILE'"; proc = Popen(cmd, shell=True, stdout=PIPE) out = proc.stdout.read().decode("utf-8") return shlex.split(out) flags += ExportFromNix()
ãããã«
Nixã¯ãæ©èœçããã°ã©ãã³ã°ã®åçã«åºã¥ããŠæ§ç¯ããããã¹ãŠã®ããã±ãŒãžãããŒãžã£ãŒã§ãããšäž»åŒµãããæè»ã§äŸ¿å©ã§ã·ã³ãã«ãªããã±ãŒãžãããŒãžã£ãŒã§ãã ç¹ã«ãC / C ++ããã°ã©ããŒã«ãšã£ãŠã¯äŸ¿å©ã§ãã ãã®èšèªã§ã¯ç©ºã®ããããåããããšãã§ããŸãã ããã䜿çšãããšãååãèŠããããå«ãã«ãããããããšãªãããããžã§ã¯ãã«ã©ã€ãã©ãªããããããŠè¿œå ã§ããŸãã ãŸããããŒã ã«å°çããæ°åè ã¯ããããžã§ã¯ãã®çµã¿ç«ãŠã«æåã®å°±æ¥æ¥ãè²»ããããšã¯ãããŸããã