@fctorial (Fctorial) posted a new topic, subject "Run shell commands in nix expressions". It read:

   Does nix have a function that can be used to run shell commands during evaluation phase? (or a general purpose `exec` function)
    
    What environment does nix use when evaluating a derivation? (chroot/normal shell)
    
    If a local file/directory is specified as the build input of a derivation, that file/directory is copied over to nix store and that copy is provided to the builder. Instead I want to use the output of a shell script i've written(which uses some local files as input) to be used as the build input of my derivation.

@jonringer (Jon) replied

    fctorial:
    
    > Does nix have a function that can be used to run shell commands during evaluation phase? (or a general purpose `exec` function)
    
    I can't see a use case for this.
    
    fctorial:
    
    > What environment does nix use when evaluating a derivation
    
    evaluation all occurs within nix code
    
    fctorial:
    
    > If a local file/directory is specified as the build input of a derivation, that file/directory is copied over to nix store and that copy is provided to the builder. Instead I want to use the output of a shell script i've written(which uses some local files as input) to be used as the build input of my derivation.
    
    Just have that be in another derivation, write it $out, then read it from the consuming derivation. You can also write arbitrary bash as part of the phases.

@7c6f434c (M R) replied

    > Does nix have a function that can be used to run shell commands during evaluation phase? (or a general purpose `exec` function)
    
    There is builtins.exec. It is guarded by a scary option etc. and for a good reason (see src/libexpr/eval.hh and primops.cc)
    
    > What environment does nix use when evaluating a derivation? (chroot/normal shell)
    
    Not sure. I guess there is some alternative plugin if it is too restrictive…
    
    > If a local file/directory is specified as the build input of a derivation, that file/directory is copied over to nix store and that copy is provided to the builder. Instead I want to use the output of a shell script i've written(which uses some local files as input) to be used as the build input of my derivation.
    
    Technically, I guess you could run a command using a copy of local files into store via ./. reference.

@fctorial (Fctorial) replied

    > Just have that be in another derivation, write it $out, then read it from the consuming derivation. You can also write arbitrary bash as part of the phases.
    
    The input directory is huge and the derivation needs to use only some of the files from that directory (file filtering logic is non trivial). Adding the directory as a dependency will be too inefficient.

@fctorial (Fctorial) replied

    I enabled the option and tried using `builtins.exec` but that gave the following error:
    [code] 
        nix-repl> builtins.exec ["echo" "hello"]
        error: undefined variable 'hello' at (string):1:1
        
    [/code]

Then @7c6f434c (M R) said

    Indeed, the builtin executes the program and parses its output. So it tries to parse «hello» and hits undefined variable. The program should output a ready to use chunk of Nix code.
    
    (It does seem to run without sandboxing)

@bjornfor (Bjørn Forsman) replied

    Try with “\“hello\””, so that nix gets a string instead of an identifier.


{'source': 'discourse', 'url': 'https://discourse.nixos.org/t/run-shell-commands-in-nix-expressions/10215', 'title': 'Run shell commands in nix expressions', 'views': 1253, 'has_accepted_answer': False, 'word_count': 550, 'like_count': 3, 'reply_count': 4, 'participant_count': 4, 'posts': 7, 'tags': '', 'author': '@fctorial (Fctorial)', 'participants': '@fctorial (Fctorial), @jonringer (Jon), @7c6f434c (M R), @bjornfor (Bjørn Forsman)', 'image_url': [None], 'created_at': '2020-11-26T05:21:24.194Z', 'last_posted_at': '2020-11-29T11:38:34.630Z'}