NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.72k stars 13.85k forks source link

How to add my .vimrc straight to config file? #26754

Closed ob7 closed 7 years ago

ob7 commented 7 years ago

Being a purist I want my vimrc settings in my configuration.nix file. How can I get config files like these into my configuration.nix file if its not an option in man configuration.nix such as adding wpa_supplicant config via netorking.wireless.networks?

bjornfor commented 7 years ago

vim_configurable reads /etc/vimrc (perhaps all vims from nixpkgs do that, but I didn't test it), so you should be able to do something like environment.etc."vimrc".source = ./myvimrc;.

There are also ways to bundle a vimrc file with a specific vim package, if you look for "vim" in nixpkgs. That way you don't have to use the global /etc/vimrc, but on the other hand it requires more up front setup.

ob7 commented 7 years ago

Can I somehow put the contents of my vimrc straight into my configuration.nix?

bjornfor commented 7 years ago

Yes.

  environment.etc."vimrc".text = ''
    syntax on
    set hidden
  '';
Mic92 commented 7 years ago

You can use the following:

{
  environment.systemPackages = [
    (pkgs.vim_configurable.customize {
      vimrcConfig.customRc = ''
        " here goes your configuration
      '';
    })
  ];
}
ob7 commented 7 years ago

Man you guys are awesome. How did you learn that?

Mic92 commented 7 years ago

From here: https://github.com/NixOS/nixpkgs/blob/master/pkgs/misc/vim-plugins/vim-utils.nix

I also ported the snipped to the official documentation now: https://github.com/NixOS/nixpkgs/commit/5041df441112a4533c62091799d864e61dbda3de

ob7 commented 7 years ago

Very nice. Will adding my vimrc like this overwrite the default vimrc nixos uses? I don't know where the default vim settings are located, but nixos by default has some pretty good defaults.

ob7 commented 7 years ago

I get error "attribute vim_configurable missing" when trying it how you said Mic92. When I try it how bjorn states I dont get any error but vim doesn't appear to be loading the custom config.

ob7 commented 7 years ago

OK I understand how bjorns answer works now. This makes file inside /etc/ called vimrc which I can link to. Nice and simple.

ob7 commented 7 years ago

But what is "vim_configurable"... Its listed in nix-env packages under VimNox and VimHugeX... but my nixos-rebuild says attribute vim_configurable is missing. Weird how vim_configurable doesn't show up when I run "nix-env -qaP"

ob7 commented 7 years ago

Never mind, just needed to swap out vim with vim_configurable in my environment packages. Thanks for your guys help. NixOS is the best, it requires the same effort as learning Arch Linux but I believe the reward is much higher with NixOS.

Mic92 commented 7 years ago

There was a typo in my example. Fixed now.

ob7 commented 7 years ago

Using bjorns example, how could I write the configuration file to my home directory instead of /etc? I want to put other configuration files into my nix configuration that normally reside in the users home directory.

ob7 commented 7 years ago

For example, how can I output a ~/.muttrc file via configuration.nix

bjornfor commented 7 years ago

configuration.nix is for configuring the system, not various $HOMEs. There is an effort to manage user homes with Nix (I think it's called "nixup" -- search for it in the bugtracker), but for now I think you'll have to manage such files yourself.

Alternatively, you can write a (system) service yourself that happens to write stuff to $HOME. But you won't find a direct NixOS option to do that.

ob7 commented 7 years ago

I suppose I could write the muttrc to /etc then symlink muttrc command to muttrc -F /etc/muttrc

Not every program has the option to pass the configuration file though.

Is there someway in nix to set symlinks within the system configuration?

Mic92 commented 7 years ago

My one is bounded to my user: https://github.com/Mic92/nixos-configuration/blob/master/.nixpkgs/config.nix#L20

ob7 commented 7 years ago

Dude nice.

ob7 commented 7 years ago

Here's mine:

{
  environment.etc."vimrc".text = ''
    syntax on
    colorscheme delek
    set expandtab
    set tabstop=2
    set shiftwidth=2
    set autoindent
    set showmode
    set hlsearch
    set ignorecase
    set ic
    set smartcase
    set wrap
    set number
    " unhighlight text and save anytime ESC is pressed
    nnoremap <esc> :noh<return>:w<return><esc>
    ""set pastetoggle=<F2>

    function! WrapForTmux(s)
      if !exists('$TMUX')
        return a:s
      endif

      let tmux_start = "\<Esc>Ptmux;"
      let tmux_end = "\<Esc>\\"

      return tmux_start . substitute(a:s, "\<Esc>", "\<Esc>\<Esc>", 'g') . tmux_end
    endfunction

    let &t_SI .= WrapForTmux("\<Esc>[?2004h")
    let &t_EI .= WrapForTmux("\<Esc>[?2004l")

    function! XTermPasteBegin()
      set pastetoggle=<Esc>[201~
      set paste
      return ""
    endfunction

    inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
  '';
}
adithyaov commented 4 years ago

Alternatively, you can write a (system) service yourself that happens to write stuff to $HOME. But you won't find a direct NixOS option to do that.

@bjornfor Could you please elaborate on this? Say I want to manage my neovim and all its config via nix expressions. The standard procedure to make a stdenv.mkDerivation? I can use my own build script to install my own package manager to manage plugins and then somehow symlink the init file to the proper path?

I haven't tried this yet. This probably will not work as described but something like this might? Please let me know your workflow with nix. Thank you.

@Mic92 https://github.com/Mic92/nixos-configuration/blob/master/.nixpkgs/config.nix#L20 This link is broken. Could you please update it?

PS: I'm not using NixOS but the nix package manager. I would like to completely port to NixOS once I get the hang of it!

bjornfor commented 4 years ago

I guess I was thinking of having a systemd.user.services.install-vimrc = {...} service that copied or rsync'ed a .vimrc into $HOME. These days I configure vim in Nix like this: https://github.com/bjornfor/nixos-config/tree/2116bdbdf16b2687d29e7b31f298adfa87b04f9a/pkgs/vim.

Note that since I wrote that comment, there is the home-manager which allows configuring vim (among other things) in Nix, per user.

bjornfor commented 4 years ago

I think both home-manager and pkgs.vim_configurable.customize (or pkgs.neovim.override) are suitable for non-NixOS use too. E.g. I use pkgs.neovim.override on Ubuntu at $dayjob.

adithyaov commented 4 years ago

@bjornfor Thank you for replying :-)

That is indeed a cool option. In the future how do I know the existence of such options? Is there a man page or if I have to investigate pkgs/xyz, where would I find such an option?

Where can I find the override option in pkgs/applications/editors/neovim/? Or is there something similar to a man page for nix expressions of a certain package?

I apologize if the questions are basic.

adithyaov commented 4 years ago

Adding to the previous comment, Below are my default.nix: default.nix:

{ pkgs ? import <nixpkgs> {} }:

let config = {
        customRC = builtins.readFile ./nvimrc;
        packages.myVimPackage = with pkgs.vimPlugins; {
                start = [
                        vim-surround
                        haskell-vim
                        supertab
                        tabular
                        vim-gitgutter
                        syntastic
                        vim-airline
                        vim-airline-themes
                        ];
                };
        };
in
pkgs.neovim.override {
        vimAlias = true;
        configure = config;
}

nix-shell default.nix puts me in a custom environment but the options for neovim don't seem to get applied. Am I doing something wrong here?

bjornfor commented 4 years ago

In the future how do I know the existence of such options?

The nixpkgs manual has a section on vim, as do the nixos wiki on vim. And there is always nixpkgs.git itself.

There is no man page per package, but editors like vim are special enough to get a place in the nixpkgs manual :-)

nix-shell default.nix puts me in a custom environment but the options for neovim don't seem to get applied. Am I doing something wrong here?

nix-shell default.nix sets up an environment for hacking on the given derivation, not an environment where the derivation is built and activated (i.e. bin/* in PATH etc.). Try nix-build default.nix instead and run ./result/bin/vim. Or install it directly with nix-env -i -f default.nix.

nixos-discourse commented 4 years ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-should-i-install-software/5668/12

Pablo1107 commented 4 years ago

@bjornfor Could you please elaborate on this? Say I want to manage my neovim and all its config via nix expressions. The standard procedure to make a stdenv.mkDerivation? I can use my own build script to install my own package manager to manage plugins and then somehow symlink the init file to the proper path?

@adithyaov I was thinking on doing something like this. Did you came up with a solution for this?

The thing is I like the declarative (and immutable) way of handling some configurations like zsh, tmux, or wm which are configs that I don't change often. But Vim is another beast, I change a lot my vimrc, just to try a plugin, or adding a command or a map for a project I'm working on, etc.

Is there anyway to have vimrc to be copied to the user's $HOME at the install process (and then supposedly be erased when the package is removed from the systemPackages option).

Currently I'm using the environment.etc option to setup a "default" configuration which I then extend in my user folder imperatively.

EDIT: Actually I didn't managed to setup etc like that because I wanted to load some plugins too. I don't know if the vim wrapper allows something like "if user has vimrc, load that instead of nixos vimrc".

aaronchall commented 3 years ago

Yes.

  environment.etc."vimrc".text = ''
    syntax on
    set hidden
  '';

environment.etc."vimrc".text doesn't seem to work because vim uses e.g. (from man vim):

       /nix/store/qx3f6nrqgsb0y0sswy4zdw0sv86dwk9x-vim-8.2.0013/share/vim/vimrc
                      System wide Vim initializations.
ob7 commented 3 years ago

@aaronchall Instead of vimrc text, build custom vim like this (see vim_configurable.customize below, the other stuff is my custom vim stuff)

{ config, pkgs, ... }:

{
  environment.shellAliases = {
    "view" = "vim -R";
  };
  programs.vim.defaultEditor = true;
  environment.systemPackages = with pkgs; [
    (vim_configurable.customize {
      name = "vim";
      vimrcConfig.customRC = ''
        let mapleader=" "
        map <C-n> :NERDTreeToggle<CR>
    set nocompatible
    set backspace=indent,eol,start
    set history=200
    set ruler
    set showcmd
    set wildmenu
    set ttimeout
    set ttimeoutlen=100
    set display=truncate
    set scrolloff=5
    set incsearch
    set nrformats-=octal
    map Q gq
    inoremap <C-U> <C-G>u<C-U>

        colorscheme delek
        set expandtab
        set tabstop=2
        set shiftwidth=2
        set autoindent
        set showmode
        set hlsearch
        set ignorecase
        set ic
        set smartcase
        set wrap
        "set number
        nnoremap <esc> :noh<return>:w<return><esc>

        if &t_Co > 2 || has("gui_running")
          syntax on
          let c_comment_strings=1
        endif

        filetype plugin indent off
        filetype plugin on

        if has("autocmd")
          augroup vimStartup
            au!
            autocmd BufReadPost *
              \ if line("'\"") >= 1 && line("'\"") <= line("$") |
              \   exe "normal! g`\"" |
              \ endif
          augroup END
        endif " has("autocmd")

        if !exists(":DiffOrig")
          command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis | wincmd p | diffthis
        endif
      '';
      vimrcConfig.packages.nixbundle = with pkgs.vimPlugins; {
        start = [
          The_NERD_Commenter
          The_NERD_tree
          haskell-vim
        ];
      };
    })
  ];
}

Yes.

  environment.etc."vimrc".text = ''
    syntax on
    set hidden
  '';

environment.etc."vimrc".text doesn't seem to work because vim uses e.g. (from man vim):

       /nix/store/qx3f6nrqgsb0y0sswy4zdw0sv86dwk9x-vim-8.2.0013/share/vim/vimrc
                      System wide Vim initializations.