nix build vs nix shell vs nix profile

tony·2025년 6월 1일
0

Tips/Review

목록 보기
3/7

Why? 왜 배움?


vagrant 패키지 설치가 안 되는 것 같아 테스트용도로 받아보려고 했다.

설치를 위해 어떤 명령어를 써야할지 헷갈려서 알아보았다.

What? 뭘 배움?


  • nix shell

    • 지정된 Nix 패키지가 포함된 환경으로 들어갈 수 있게 해줍니다.

    • 해당 환경 내에서 인터랙티브 셸을 엽니다

      # 'hello'와 'cowsay' 패키지가 포함된 환경으로 진입합니다
      › nix shell nixpkgs#hello nixpkgs#cowsay
      
      # 이제 hello 명령어를 사용할 수 있습니다
      › hello
      Hello, world!
      
      # cowsay도 사용할 수 있습니다
      › cowsay "Hello, world!"
       _______
      < hello >
       -------
              \   ^__^
               \  (oo)\_______
                  (__)\       )\/\
                      ||----w |
                      ||     ||
  • nix run

    • 지정된 Nix 패키지로 환경을 구성한 뒤, 해당 패키지를 바로 실행합니다.

    • 패키지를 시스템 환경에 설치하지 않고도, 실행 가능한 프로그램을 직접 실행합니다

      # hello는 시스템에 설치되어 있지 않습니다
      › hello
      hello: command not found
      
      # 'hello' 패키지로 환경을 구성하고 바로 실행합니다
      › nix run nixpkgs#hello
      Hello, world!
  • nix build

    • 소프트웨어 패키지를 빌드합니다.

    • 빌드 결과물을 가리키는 result라는 심볼릭 링크를 현재 디렉터리에 생성합니다.

    • nix build -L .#my-custom-pkg는 로컬 플ake에서 패키지를 빌드합니다.

      -L 옵션은 자세한 로그를 보여줘서, 경고나 오류를 확인할 수 있게 해줍니다.

      # 'nixpkgs' 'ponysay' 패키지를 빌드합니다
      nix build "nixpkgs#ponysay"
      # 빌드된 'ponysay' 명령어를 사용합니다
      › ./result/bin/ponysay 'hey buddy!'
       ____________
      < hey buddy! >
       ------------
           \
            \
             \
             ▄▄  ▄▄ ▄ ▄
          ▀▄▄▄█▄▄▄▄▄█▄▄▄
         ▀▄███▄▄██▄██▄▄██
        ▄██▄███▄▄██▄▄▄█▄██
       █▄█▄██▄█████████▄██
        ▄▄█▄█▄▄▄▄▄████████
       ▀▀▀▄█▄█▄█▄▄▄▄▄█████         ▄   ▄
          ▀▄████▄▄▄█▄█▄▄██       ▄▄▄▄▄█▄▄▄
          █▄██▄▄▄▄███▄▄▄██    ▄▄▄▄▄▄▄▄▄█▄▄
          ▀▄▄██████▄▄▄████    █████████████
             ▀▀▀▀▀█████▄▄ ▄▄▄▄▄▄▄▄▄▄██▄█▄▄▀
                  ██▄███▄▄▄▄█▄▄▀  ███▄█▄▄▄█▀
                  █▄██▄▄▄▄▄████   ███████▄██
                  █▄███▄▄█████    ▀███▄█████▄
                  ██████▀▄▄▄█▄█    █▄██▄▄█▄█▄
                 ███████ ███████   ▀████▄████
                 ▀▀█▄▄▄▀ ▀▀█▄▄▄▀     ▀██▄▄██▀█
                                      ▀  ▀▀█
  • nix develop

    • 주어진 도출(derivation)의 빌드 환경을 제공하는 bash 셸을 실행합니다

      # 현재 디렉터리의 플ake에서 기본 패키지의 빌드 환경이 설정된 셸을 시작합니다:
      nix develop
      
      # 외부 플ake에 정의된 Nix 개발 환경으로 진입합니다
      nix develop "https://flakehub.com/f/DeterminateSystems/zero-to-nix/*#example"

How? 어떻게 씀?


필자는 vagrant 가 제대로 설치 되는지를 확인해보기 위해 nix build 를 호출하였다.

다만 설치하고자 하는 pkgs 가 unfree pckgs 인 경우 allowUnfree 플래그를 활성화해주지 않으면 빌드가 불가능하다.

~/my-nixos main
❯ nix build nixpkgs#vagrant
error:
       … in the condition of the assert statement
         at /nix/store/wkjmkjrfr9ik1blw8zmv9g5xjl412py8-source/lib/customisation.nix:419:9:
          418|       drvPath =
          419|         assert condition;
             |         ^
          420|         drv.drvPath;

       … while evaluating the attribute 'handled'
         at /nix/store/wkjmkjrfr9ik1blw8zmv9g5xjl412py8-source/pkgs/stdenv/generic/check-meta.nix:643:9:
          642|         # or, alternatively, just output a warning message.
          643|         handled = (
             |         ^
          644|           if valid == "yes" then

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: Package ‘vagrant-2.4.3’ in /nix/store/wkjmkjrfr9ik1blw8zmv9g5xjl412py8-source/pkgs/by-name/va/vagrant/package.nix:135 has an unfree license (‘bsl11’), refusing to evaluate.

       a) To temporarily allow unfree packages, you can use an environment variable
          for a single invocation of the nix tools.

            $ export NIXPKGS_ALLOW_UNFREE=1

          Note: When using nix shell, nix build, nix develop, etc with a flake,
                then pass --impure in order to allow use of environment variables.

       b) For nixos-rebuild you can set
         { nixpkgs.config.allowUnfree = true; }
       in configuration.nix to override this.

       Alternatively you can configure a predicate to allow specific packages:
         { nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
             "vagrant"
           ];
         }

       c) For nix-env, nix-build, nix-shell or any other Nix command you can add
         { allowUnfree = true; }
       to ~/.config/nixpkgs/config.nix.
~/my-nixos main

따라서 이를 위해 home.nix 에서 ~/.config/nixpkgs/config.nix 에 대한 dotfile 을 만들어 symbolic link 로 연결하게끔 하였다.

# ./dotfiles/nixpkgs/config.nix
{
  allowUnfree = true;
  allowUnfreePredicate = pkg: true;
}

{ lib, pkgs, config, ... }: 
{
  # Enable Home Manager
  programs.home-manager.enable = true;
 
  # Set env automatically
  targets.genericLinux.enable = true;

  # Import dotfiles
  home.file = {
    ".config/nix".source = ./dotfiles/nix;
    ".config/nixpkgs".source = ./dotfiles/nixpkgs;
    ".config/nvim".source = ./dotfiles/nvim;
  };

  # Import all modularized configurations
  imports = [
    # Infra
    ./modules/infra.nix
    
    # Dev
    ./modules/language.nix

    # Shell
    ./modules/apps.nix
    ./modules/nvim.nix
    ./modules/zsh.nix
    ./modules/shell.nix
  ];
}

이후 아래와 같이 호출하면 설치가 가능하다.

nix --impure build nixpkgs#vagrant

Reference


https://nixos-and-flakes.thiscute.world/development/intro#nix-build

https://nixos-and-flakes.thiscute.world/other-usage-of-flakes/the-new-cli#nix-shell

https://nixos-and-flakes.thiscute.world/other-usage-of-flakes/the-new-cli#nix-run

profile
내 코드로 세상이 더 나은 방향으로 나아갈 수 있기를

0개의 댓글